Currently the API supports a [near] filter relative to a lat, lng geopoint, such as
/homes?filter[where][location][near]=lat,lng
You can additionally support a result count, such as
/homes?filter[where][location][near]=lat,lng&filter[limit]=3
I'd like to request adding a filter by radius capability as follows:
filter[where][location][radius]=lat,lng,10m
where m indicates miles and k indicates kilometers ( 10m = 10 miles; 10k = 10 kilometers; default 10 = 10 miles). This assumes you can continue to filter[limit] the result count.

Another approach could be
/homes?filter[where][location][near]=lat,lng&filter[radius]=10m&filter[limit]=25
The filter[radius] would be ignored if there wasn't a prior geopoint filter [near].
This has been suggested elsewhere (I think in a PR to the mongo connector). I don't think radius is appropriate though. I'd suggest something similar to mongo's $maxDistance.
I can see that and it simplifies the request to better match existing db support.
/homes?filter[where][location][near]=lat,lng&filter[distance]=8046.72&filter[limit]=25
where the distance is in meters (double value; 5 miles == 8046.72 meters).
@rptally @ritch hey guys, what else needs to be done to have this merged to master?
@rptally @ritch hey guys, what else needs to be done to have this merged to master?
@eugenehp
Which PR are you referring to? I'm not aware of a PR that implements $maxDistance as discussed here. If you can point me to one I'll try and get it merged.
It seems like maxDistance is already available: https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/geo.js#L17
And as far as I understood from the code: https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/dao.js#L1448-L1450 and https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/geo.js#L28-L79 filtering by distance is done as a post-processing step after retrieving all the results using other filters. Unless buildNearFilter is implemented for data source.
Here is a question then, for mongodb in particular, which seems not to have buildNearFilter implementation yet. If database has undefined number of records matching other filtering criterias (may be two, or may be millions), and only two of them are matching distance criteria, what would be the proper way to get only them and not others without damaging performance?
@voitau :+1:
@ritch let us know, what do you think about previous comment. thanks!
filtering by distance is done as a post-processing step after retrieving all the results using other filters. Unless buildNearFilter is implemented for data source
I think you've nailed it here: mongo needs to implement buildNearFilter
@superkhau
So I hate to ask a tech question here - but apparently maxDistance is supported - but I'm not sure we actually document _how_ you would use it in a REST-based URL filter. Got an example?
@cfjedimaster
I avoided going via a REST URL filter way. Rather I have four URL parameters (lat,lng,radius and radiusType) to a remote method call - radius defaulted to 2000 and radiusType to meters, it works.
Notice the maxDistance and unit in the GeoPoint query!
var loopback = require('loopback');
module.exports = function(Cafe) {
Cafe.remoteMethod('nearLocation',
{
accepts: [
{arg: 'lat', type: 'number', required: true},
{arg: 'lng', type: 'number', required: true},
{arg: 'radius', type:'number', required: false, default:2000},
{arg: 'radiusType', type:'string', required: false, default:'meters'}
],
http: {path: '/nearLocation', verb: 'get'},
returns: {arg: 'cafes', type: 'Object'}
});
Cafe.nearLocation = function(lat, lng, radius, radiusType, cb){
var here = new loopback.GeoPoint({lat, lng});
Cafe.find( {where: {location: {near: here, maxDistance:radius, unit:radiusType}}, limit:10},
function(err, nearbyCafes) {
console.info(nearbyCafes); // [Cafe, ...]
cb(null, nearbyCafes);
}
);
}
};
Thanks - I finally figured it out myself. :)
@cfjedimaster Would you mind to share it with us? ;-)
@terehov I have given an example above. It has worked for me. In my app I let the caller define the radius type (meters, miles etc as distance unit) and radius value ( actual max distance in specified units), while picking up the lat long from phone's geolocation
@terehov I blogged about it here: https://strongloop.com/strongblog/working-with-geographical-data-in-your-api/
Hi, is this issue fixed? I see the PR merged and also @cfjedimaster blog.
If not, please let me know. Thanks.
Closing. Issue looks resolved. Please re-open if https://github.com/strongloop/loopback-connector-mongodb/pull/68 did not resolve this.
Most helpful comment
@terehov I blogged about it here: https://strongloop.com/strongblog/working-with-geographical-data-in-your-api/