Spring Data - MongoDb finding nearest locations around a route - java

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

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.

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

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/

Related

How do I generate route based on distance in android

I am looking for a program that can produce routes based on distance criteria. For example, I give a start point and an endpoint that are the same and I want 5-10 different routes which start and end there but are about 5 miles long. Is there anything out there that can do that?
I know that Google Maps can do that with just a start and end but no distance parameter. I just want to have distance be a parameter and have the start and end points be the same.
Please check this link for reference: routeshuffle

How to Show path from source and destination in Android app and Navigate the user within the App

I'm trying to develop an Android app and I'm able to fetch the destination lat and long from JSON response. I manged to collect the users current lat and long.
Now How can I show a route or Line from source and destination and update the users current location for every few seconds.
Is it possible to do this and show the ETA and distance between the markers?
Thank You
#Uday, the plotting of polyline and calculating the direction between two locations can be done using Google Maps Directions API.
The Google Maps Directions API is a service that calculates directions between locations using an HTTP request.
In this tutorial, you'll see how to use Google Direction API and Places API. It will also guide you on the implementation of API and Google HTTP Client Library for Java. For the ETA and distance between the markers Google Direction response has an object called Distance and Duration within "LEGS" array:
A DirectionsLeg defines a single leg of a journey from the origin to
the destination in the calculated route. For routes that contain no
waypoints, the route will consist of a single "leg," but for routes
that define one or more waypoints, the route will consist of one or
more legs, corresponding to the specific legs of the journey.
distance indicates the total distance covered by this leg, as a Distance object
duration indicates the total duration of this leg, as a Duration object
Here is a related SO question about travel time. It explains how to get the duration of each leg and distance until the end marker point and how to parse the distance and duration in your app.

how to match two routes from Google Directions API response?

I building an application that matches the route of a user with the routes of all others users. Routes of all users are collected using google directions api. i am not able to figure out a efficient solution to match the routes.
example: User 1 travels from A to B. User2 from L to M and User 3 from X to Y. Now, i have the direction responses from google map api(preferably json objects) which has latlng info of all the points in the three routes. Now i need to find if route A to B matches with other two routes either full or partially.[ By full,i mean that they have either same origin or destination and by partial, i mean if any two routes have some part common or atmost 2 kilometers part].
I can do this by comparing each point in route A to B with points in other routes.But this is a tedious task consuming all my resources and timetaking.
please help me with efficient solution.
I would be glad if there is any algorithm to simplify this task?
I can do this by comparing each point in route A to B with points in other routes.
This is just about the only way.
The only refinement is to Simplify the lines first. This reduces the amount of points you have to compare.
Do beware of the Google Maps API terms, you can't take the data you obtain from the API outside of a Google Maps application. So you should be fine as long as you keep the data within a Maps API website, but dont export the data anywhere.

How to find nearest City of out of a selected set

I need to get the nearest city out of a selected set of cities.
Our company has a list of subsidiaries (some 100 in my country). We get around 3000 requests a day. This requests should be assigned to the subsidiaries (by geographical distance).
Is there an API to do this?
The best would be a (java) GoogleMaps API or similar webservice.
Best Regards,
Christian.
What I would do is to construct a Voronoi diagram of your subsidiaries, based on geographical distance and store that diagram in the form that can be used in your code. Then, look for the containing cell for each request and that will tell you which subsidiary is the closest one.
If you really want to make it precise, you could use OSM's road network to construct the diagram based on the driving distance, not simply geographical one.
Get the coordinates from Nominatim, it should be straight forward to make requests from a java application.
Calculate the great circle distance for every city to every city. I have to admit that the result might by an 300 by 300? array. However it may contain only integer. Hold it in memory for future requests.
Find the entry with the lowest number in the row or column.
Bit of an old question and maybe too late an answer for you.
A good approximation for speed concerns where absolute precision is not of the essence is to draw a rectangle around a point (the one you need to find the closest subsidiaries here). That rectangle would natively have NE and SW coordinate boundaries (or NW/SE).
To find the closest subsidiaries, ones need to find all of which the NE coordinate is "less" than that of the rectangle and "more" than that of the SW boundary.
I am quoting "more" and "less" because they might mean differently based of where on earth you are.
I wrote https://github.com/grumlimited/geocalc for my own need a few years back. Take a look at the section about names "BoundingArea".

Applying Graph Theory in shortest path calculation on a offline map

How do I utilize the concept of Graph Theory in finding the shortest path in an offline map in Android? I am using osmdroid library and mapnik map tiles to display a map. My problem right now is how do I drawing and calculating the shortest path on the map basing from the user's last known location to his destination and the line should go along with the roads.
Can I utilize the geocoordinates?
You can use GraphHopper (warning: I'm the dev) in combination with mapforge (offline renderer).
And with this location to id index you can query real world lat,lon coordinates and you'll get id's of the graph. With that id's you can fetch the real lat,lon via the Graph interface.
Let me know if you need more info for integrating this with mapforge. For the desktop there is alreay a rough implementation.

Categories

Resources