How to find nearest locations around a route in MOngoDB Spring Data

Published on Author Code Father

The route information from the maps googleapi is broken down into steps that have a start location and end location with latitude/longitude coordinates.

Computing the distance of the points in your model to all the start/end locations in the route would give you a measure of how far the point is from the route. The minimum ‘distance’ from the route start/end points would be the nearest location to the route.

You can optimize the computation by discard any points when the computed distance is greater than the previous minimum cumulative distance.

——————————————————-or ————————–

First, you need to get this result and then put it in some array that contains Lat+Lng.

Second, create a method in your repository that contains the parameter you want search by with the term “Within”.


List<MyObject> findByLocationWithin(Box box);

This method represent the respective condition:

{"location":  {"$geoWithin":  {"$box":  [ [x1, y1], [x2, y2]}}}

If you need more information you access the follow link: