How to find the distance between two points along a specific route? - android

I'm developing an android app for my college that helps students track the current location of the college bus and also give them an estimated time when the bus might reach them. As of now I get the current location of the bus(via a device present on the bus) and the location of the student. I'm stuck at the point where I have to find the distance between the two GPS co-ordinates. I have to find the distance between the bus and the student "along a specific route" (i.e the path the bus takes). Using Google Map APIs will not always give the distance along the route I want. How do I find the distance between two points along a specific route?

The Google Maps Android API Utility Library has a range of applications useful for your case, including calculating distance between two points, by using the computeDistanceBetween(LatLng from, LatLng to) method from the SphericalUtil class.
Also check this link.
Google Distance Matrix API provides travel distance and time for a matrix of origins and destinations.
Or you could try Google Directions API.
Also, I believe this guy had the same doubt of yours.
Hope this helps you.

I would it solve this way:
1) The direct line of sight distance betweent two GPS points is simple, simply use Location.distanceBetween() method, you will find hundres of posts here on SO.
2) The length of the route.
This part is more difficult. If you are not happy with the result of google:
Store your bus routes as rough sequence of lat/lon coordinates (a poly line). A point per street corner should be sufficient.
Then find the nearest point on the route, a simple brute force search should be sufficent for your task.
Then simply iterate from found route point till destination point, and sum up using the distanceBetween() method.

Related

I want to retrieve the latitude and longitude of a location shown in google map route between two locations

I have lat and long of some locations in JSON. I want to spot whether these locations are present in a route shown by google maps.
Here are the steps I'd consider to achieve the effect you want:
Query for a DirectionResponse https://developers.google.com/maps/documentation/directions/intro#DirectionsResponses
For each polyline of each step of each leg, test whether your locations are close to the line. For simplicity you can also use the overview_polyline. Unfortunately this is not simple to do out-of-the-box using the Maps API alone. Check this thread for inspiration on a manual implementation. Find a point in a polyline which is closest to a latlng
Alternatively, convert the polylines to GeoJSON, and process using a spatial library such as JSTS or Turf. E.g. the latter provides a pointOnLine function.

How can I use Graph data structure to generate route with exact distance using Google API in Android?

I need help with my final CS Project. Basically, I have to write an Android app that the user can specify a starting location and distance desired. It is going to generate a route that is exactly the distance of the input that the user can walk/ run and brings the user back to the starting location, using the Graph data structure and Google API.
I have my Google map and Android app but I’m not sure where to start with storing data using Graph ( I'm not sure if I should even use this data structure) and how to calculate the distance input. Any bits of advice/ help will be appreciated!
Okay your question is very tricky but still let me answer from what I got.
Use google map api, google places api, place picker or id and google maps json api for routes and distance.
let's take an example like you want walk or run 4kms from your places and comeback.
Case 1: if you want to opt for the same route from which you went to come back.
like you went from a-b then again you are coming bak from b-a.
for this case you can use Google maps geofencing and get the place inside a minimum raidus and make an algo to get the distance from your places.
if teh distance is half of the distance from your places then pick the most approximate place.
Use google maps json to get the places distance and google place picker for the location of nearby location object.
Case2: If you donot want to come from the same route through which you went
by, then select the nearest location object and get the longest route and follow
the same algo and procedure to get the distance.

Difference between Google Map Distance and Directions API

Google provides APIs for 1. Distance Matrix and 2. Direction service
I tried to search about difference between 1 and 2
My task is "When user moves from Source S to Destination D
using some path. We need to show the actual path user has traversed
on Google Map"
What is best way to accomplish it ?
Using 1. Distance Matrix APIs or 2. Direction service
According to Google Maps API picker
You use Directions service for getting directions from origin to destination location using various forms of transport: walking, driving, cycling, public transit. Get a route from point A to point B.
For Distance Matrix it calculate the travel distance and travel time for multiple origins and destinations, optionally specifying various forms of transport: walking, driving, cycling.
For your case, I will suggest Directions service since you don't need travel time.
I got answer to my question.
Google Maps V2 provides 2 sets of APIs for developers
1. Distance Matrix APIs : this is useful for computing distance(walking/travelling via bycycle,bus), cost of travel, time of travel
But this is not useful for this scenario.
2. Direction Service : this is useful for this scenario.
Through direction service, we can get the path of User on Google Map in Real Time
If we get the path through Distance Matrix, then Google Map returns
the shortest established path between S Source and D
Destination, this is not desirable in this scenario. User may not use
shortest path due to some trafic, personal reasons and Diatance Matrix
API output fails here.
So, I should use Direction Service API which give me real time Output for User location on Google Map
Thank you for your replies

Trace the location only on Road in Google maps V2 android

I want to show the current location of the user only on road(not beside of the road) while moving. I have used the LocationManager to get the current location. But sometimes I am getting the location beside the road, with this, the location point on map is showing beside the road. Actually, I have to show his location on Road(if he is beside the road) only. I searched lot for this but didn't get any idea.
Example: How I am getting the current locations on map.
m
A---n-------B
here, A is start point
B is end point
n is on road(current location for sometime)
m is beside road(current location)
But I would need the m also should be on road.
Please help me on this..
The google directions service will calculate from nearest road so calculate directions from current location to current location - returned starting location will be on a road.
But what if user genuinely is off-road in a shopping mall carpark for instance?
For that you need to use road API which will give you the path upon the road otherwise you will not get the accurate path.
Snap to Roads
The Google Maps Roads API takes up to 100 GPS points collected along a route, and returns a similar set of data, with the points snapped to the most likely roads the vehicle was traveling along. Optionally, you can request that the points be interpolated, resulting in a path that smoothly follows the geometry of the road.
Check Details Here
You need to call like this
https://roads.googleapis.com/v1/snapToRoads?path=-35.27801,149.12958|-35.28032,149.12907|-35.28099,149.12929|-35.28144,149.12984|-35.28194,149.13003|-35.28282,149.12956|-35.28302,149.12881|-35.28473,149.12836&interpolate=true&key=YOUR_API_KEY

Find POI along a Google Maps route? (Android)

doing "points of interest along a route" in google maps
This would fit perfectly to my question...... But its not suitable for an android activity :/
So I start copiing the question now... just wait :D... aargh! Done!
I need to allow travelers to use google maps to plot a route, and then
query my database of points of interest (let's say, McDonald's
locations) and then show all of these locations that are within a mile
or two of the route they will be taking. The question is, how do I
efficiently take the "driving directions" information that comes back
from google (essentially an array of lat/long pairs), and turn that
into an sql query to get locations that fall within a certain distance
from the route?
...and then put the whole thing into my android app?
I dont get it :(
First things first:
1) do you have latlng (geolocations) variable type in your database?
If answer(1) == yes:
Calculate and query your database: D²=(Xb-Xa)²+(Yb-Ya)² along each of your Xa,Ya route points to discover the POIs (Ya,Yb) within Distance = D.

Categories

Resources