how to match two routes from Google Directions API response? - java

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.

Related

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

Google Distance Matrix API - train distance

I'm using the Google Distance Matrix API with Java. I want to calculate the train-distance between two cities. One of the cities has no train station. The problem is, google doesn't just calculate the train-distance. It always adds the car-distance from the city without trainstation to the next city with a train station. But for my project i just need the train-distance.
This is a part of my code:
DistanceMatrix matrix = DistanceMatrixApi.newRequest(context)
.origins(origins)
.destinations(destinations)
.mode(TravelMode.TRANSIT)
.transitModes(TransitMode.TRAIN)
.units(Unit.METRIC)
.await();
In the documentation I can't find a solution for this problem.
The Google Maps search algorithms were designed to solve real-world transportation routes, which means that sending a train to a location where the passenger cannot get off is not feasible and therefore not reflected in the routes. I cannot think of any way to extract that information from Google Maps, except possibly doing a direct Directions query, though I doubt it would have different results, since it uses (as far as I can tell) the fundamentally same algorithm for all of its services.
Using a database such as OpenStreetMap (using free, limitless APIs like Overpass to access via HTTP in Java) is probably the best solution. If you want a quick fix, and you are capable of identifying cities without train stations, taking the linear distance between the two cities probably returns a reasonable (though deflated) estimate using the Geometry Library.
Sorry there is not a solution I can see, but this is the best I have.

How to get Google Directions to stay on a specified route to get somewhere?

I'm developing an Android app, but I'm not sure how to do the next thing: there is one biking route that we've chosen. The users should get directions for continuing along this biking road.
So Google Directions should stay on my route and not choose a road that is not within my route to get to the end of the biking road.
Any idea how this could be done?
If I understand you right, you wanna have a route from Google Directions, which should match your determined route.
Well, it could be hard to manage that because Directions is designed to find the shortest path between two points. One way is to use the option of waypoints, to limit the suggestions of the API. Simple said, don't use just a origin and a destination. Use as much waypoints as you have to between the origin and the destination to get the nearest route possible.
Getting the exact route as you think of it is just big luck.

get distance between two places

I actually am trying to find the distance between two places, I am new to this. I came to know about openstreetmap. But I am not sure what to do.
I found a traveling salesman application that uses osm to route.
But here my question is how to get a osm between places .e.g. "http://www.openstreetmap.org/api/0.6/map?bbox=10,30,10,30" this gives me the map of the box (10,30,10,30) .. but if I want to get the map between leeds and sheffield what should be my query.
Or is there any other way to find the distance.(As I am a student I want some open source way for my academic project..)
please suggest..
You should consider implementing this by yourself.
First, I suggest you to use the Overpass API to do your queries.
Then implement this algorithm over the ways you get from the Overpass API :)
For calculating the routing distance between two places you can use one of the various online or offline routers. There is also a list of various information for routing developers containing a list of libraries and other information.
If you have to obtain the location of a specific address before doing the actual routing you can use Nominatim.

android automatic route for pedestrians

I would like to develop an Android application that allows users (pedestrians) to follow an automatically generated route.
Users would specify how far they wish to walk, whether they must return to their stating position, avoid hills, dangerous roads, not cross any roads, how fast they walk on avergae, etc.
i have been searching for a java based library or service that generates a route but have failed.
can anyone recommend a java library that will automatically generate a route?
Interesting idea. I've never seen any library or available implementation that matches your requirements exactly. What you are attempting to do, though, is basic route optimization.
Look at some combination of the google directions API (https://developers.google.com/maps/documentation/directions/) and a set of pre-defined (that you set up) waypoints around the city. The waypoints represent nodes in your "graph" and the edges are routes between them. Waypoints have coordinates so you can map them. Then you assign weight (preference) to each edge and use google to calculate the distance between the waypoints. Extract the routes that match your distance preference and then pick the one with the best "weight" preference.

Categories

Resources