aymeba
aymeba

Reputation: 934

Spring Data - MongoDb finding nearest locations around a route

I've a model contains geojson points. Finding nearest with spring data is quite easy but how can retrieve nearest location for a giving route?

I am getting the route information from google:

http://maps.googleapis.com/maps/api/directions/xml?origin=48.208174,16.373819&destination=48.340670,16.717540&sensor=false&units=metric&mode=driving

Upvotes: 10

Views: 1709

Answers (3)

DhruvPathak
DhruvPathak

Reputation: 43235

Google maps api returns 'steps' of the route, which has coordinates of the edges of that stretch. You can use those edges to create extrapolated points on that straight stretch. Lets call them p1,p2,p3,p4...pN. Then you run $near query in your database for these points, you will get nearest locations around that route.

enter image description here

Open street map database gives information of coordinates of the route, which you can use to supplement your data. Detailed answer here : Get exact geo coordinates along an entire route, Google Maps or OpenStreetMap

Upvotes: 2

Tiarê Balbi
Tiarê Balbi

Reputation: 1509

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".

Example:

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:

http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongodb.repositories.queries

https://docs.mongodb.org/manual/reference/operator/query/box/

Upvotes: 1

Ron Puri
Ron Puri

Reputation: 94

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.

Upvotes: 3

Related Questions