Ich baue eine API, die Standorte speichert (mit ihren Koordinaten), und ich möchte sie basierend auf ihrer Entfernung zum Benutzer abfragen.nodejs: Entfernung vom Standort zum Benutzer berechnen
In meiner Ansicht-Controller, ich habe diesen Code:
var geolocation = angular.module('geolocation', []);
navigator.geolocation.watchPosition(render);
var lat, lon = 0;
function render(pos) {
lat = pos.coords.latitude;
lon = pos.coords.longitude;
console.log(lat +","+ lon);
}
, dass der Benutzer aktuelle Position zurückkehrt. Mein Schema wird wie folgt modelliert:
var LocationSchema = new Schema({
locationName: {
type: String,
Required: 'Enter the name of the location.'
},
description: {
type: String,
Required: 'Enter the description of the location.'
},
geolocation: {
type: { type: String, default:"Point" },
coordinates: [Number],
},
Und in meinem APIcontroller, habe ich versucht, dies zu tun:
exports.find_all_locations_near = function(req, res) {
Location.find({
geolocation:
{
$near :
{
$geometry: { type: "Point", coordinates: [ lat, lon ] },
$minDistance: req.body.minDistance,
$maxDistance: req.body.maxDistance
}
}
}), function(err, location) {
if (err)
res.send(err);
else if (location =='')
//res.json({ message: 'No location was found.' });
console.log('No location was found.');
else
res.json(location);
};
}
ich die minimalen und maximalen Entfernungen auf diese Weise einreichen:
<div class="form-group">
<label>Min Distance</label> <input type="text" class="form-control text-center" ng-model="formData.minDistance"><br>
</div>
<div class="form-group">
<label>Max Distance</label> <input type="text" class="form-control text-center" ng-model="formData.maxDistance"><br>
</div>
Aber nichts passiert. Ich bekomme 0 Ergebnisse. Was mache ich falsch? Wie kann ich den Abstand zwischen dem Benutzer und einem Standort berechnen und den Wert für jede Zeile zurückgeben?