Need to send values of long and lat as one array in input, then save it in the database. Found a lot of examples that explain how to handle arrays, but most of them are in PHP. Here's the code:
HTML
<html>
<head>
<script>
function geoFindMe() {
var coordinates = document.getElementById("coordinates");
if (!navigator.geolocation) {
alert("Geolocation is not supported by your browser");
return;
}
function success(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var coordinates = [longitude, latitude];
document.getElementById("coordinates").value = coordinates;
}
function error() { alert("Unable to retrieve your location"); }
navigator.geolocation.getCurrentPosition(success, error);
}
</script>
</head>
<body>
<label for="pos">Allow location service</label>
<input name="pos" id="coordinates" type="checkbox" onclick="geoFindMe()" value="coordinates[]">
</body>
</html>
app.js
app.post("/", (req, res) => {
const newUser = new data({
geometry: {
type:'Point',
coordinates: req.body.coordinates
}
});
});
data.js
const FindMyLocation = new Schema({
geometry: {
type: {
type: String,
default: 'Point'
},
coordinates: {
type:[Number],
index: '2dsphere'
}
}
});
Thanks for your attention.