angularjs - nodejs: calculate distance from location to user -
this question exact duplicate of:
i'm building api stores locations (with coordinates), , want query them based on distance user.
in view controller, have 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); }
that returns user current position. schema modeled this:
var locationschema = new schema({ locationname: { type: string, required: 'enter name of location.' }, description: { type: string, required: 'enter description of location.' }, geolocation: { type: { type: string, default:"point" }, coordinates: [number], },
and in apicontroller, tried this:
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 found.' }); console.log('no location found.'); else res.json(location); }; }
i'm submitting min , max distances way:
<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>
but nothing happens. 0 results. doing wrong? also, how can calculate distance between user , location , return on each row?
please note while storing coordinates, longitude should come first , latitude. if looks confusing, you store coordinates embedded document keys lng
& lat
.
Comments
Post a Comment