How to calculate the walking distance between two points in Android?
For example:
static final LatLng Point_One = new LatLng(41.995908, 21.431491);
static final LatLng Point_Two = new LatLng(41.996097, 21.422419);
Now, the distance between these points on maps.google.com is 950 meters. And the Location.distanceBetween is returning the air line distance between these two points, I need the Walking distane. Thanks in advance.
PS: I am using google maps Api v2
Just request the maps API as documented in The Google Distance Matrix API
So, your request to Maps API is :
http://maps.googleapis.com/maps/api/distancematrix/json?origins=41.995908,%2021.431491&destinations=41.996097,%2021.422419&mode=walking&sensor=false
You just have to query this url from your application.
I don't think you can get a walking distance between two points (other than as a straight line). You could plot a track of the walk (GPX) and then calculate distance. You can do this with an App as you walk or on a map.
Related
I was just wondering as to how to draw route direction(two point) of Google Maps in order for it to work offline. We have already downloaded offline Google Maps and then want navigation but do not know how to.
I was thinking of creating a navigation system with offline Google Maps, but I don't know how to draw route direction offline Google Map to work offline then embed it within my own application.
I have already used #mapbox Sdk, but my issue was I have downloaded offline location in Google Maps, after this location search any direction used two point direction in map, so I can drawline easily.
Using this : https://www.mapbox.com/android-sdk/examples/offline-manager/
Please help me on this one..
Could you clarify how exactly you are getting Google Directions API to work offline, to my knowledge the API only works online? Drawing the route can be done in a few different ways. The simpliest would be to convert the linestring the directions API gives you into multiple positions and then feed them into the polyline:
private void drawRouteLine(DirectionsRoute route) {
List<Position> positions = LineString.fromPolyline(route.getGeometry(), Constants.PRECISION_6).getCoordinates();
List<LatLng> latLngs = new ArrayList<>();
for (Position position : positions) {
latLngs.add(new LatLng(position.getLatitude(), position.getLongitude()));
}
routeLine = mapboxMap.addPolyline(new PolylineOptions()
.addAll(latLngs)
.color(Color.parseColor("#56b881"))
.width(5f));
}
I was trying to draw a poly line on the road but it gets deviate from the road and the poly line draws over the building near by the road.After a long search I got this link ("snap to road").where I need to send the set of latitude and longitude with pipe line separator along with the road api and it will return the accurate lat long which near to the road points.But I just want to draw on live which mean every single lat long should be accurate.
HashMap<String, String> map = new HashMap<String, String>();
map.put(Constants.URL,"https://roads.googleapis.com/v1/snapToRoads?path="
+d + "," +e+"&key=API KEY");
What I did is I'll call the road api whenever the lat long get changes on my android mobile so it will return the accurate lat long to me but there is a limitation in google that I can only request the api up to 2500/day requests.
So is there any alternate way to achieve my requirement?.
This might help you, I wrote an extensive answer for someone who had the similar issue with. Check it out, hope it would help you too
https://stackoverflow.com/a/11357351/975959
I am developping an application using google maps , and I want to center the map on a certain country , let's say Tunisia , how to do this ?
Thanks.
Assuming you are using Google Maps V2 (which you should be), Google Maps has a number of Camera Update methods which are used to move the camera (and hence, what the user sees).
In this case, you'd want to determine the latitude and longitude of the target location, then use CameraUpdateFactory.newLatLng.
Then, you'd use:
GoogleMap map = ...;
map.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(lat,lng));
where lat and lng are measured in degrees (per the LatLng documentation).
I need to find the estimate drive time from one place to another. I've got latitudes and longitudes for both places but I have no idea how to do that. Is there is any API for that.
help thanks.
yes you get the time and distance value as well as many like direction details in driving, walking etc mode. all you got from the google direction api service
check our this links
http://code.google.com/apis/maps/documentation/directions/
Location location1 = new Location("");
location1.setLatitude(lat);
location1.setLongitude(long);
Location location2 = new Location("");
location2.setLatitude(lat);
location2.setLongitude(long);
float distanceInMeters = location1.distanceTo(location2);
EDIT :
//For example spead is 10 meters per minute.
int speedIs10MetersPerMinute = 10;
float estimatedDriveTimeInMinutes = distanceInMeters / speedIs10MetersPerMinute;
Please also see this, if above not works for you:
Calculate distance between two points in google maps V3
Deprecation note The following described solution is based on Google's Java Client for Google Maps Services which is not intended to be used in an Android App due to the potential for loss of API keys (as noted by PK Gupta in the comments). Hence, I would no longer recommened it to use for production purposes.
As already described by Praktik, you can use Google's directions API to estimate the time needed to get from one place to another taking directions and traffic into account. But you don't have to use the web API and build your own wrapper, instead use the Java implementation provided by Google itself, which is available through the Maven/gradle repository.
Add the google-maps-services to your app's build.gradle:
dependencies {
compile 'com.google.maps:google-maps-services:0.2.5'
}
Perform the request and extract the duration:
// - Put your api key (https://developers.google.com/maps/documentation/directions/get-api-key) here:
private static final String API_KEY = "AZ.."
/**
Use Google's directions api to calculate the estimated time needed to
drive from origin to destination by car.
#param origin The address/coordinates of the origin (see {#link DirectionsApiRequest#origin(String)} for more information on how to format the input)
#param destination The address/coordinates of the destination (see {#link DirectionsApiRequest#destination(String)} for more information on how to format the input)
#return The estimated time needed to travel human-friendly formatted
*/
public String getDurationForRoute(String origin, String destination)
// - We need a context to access the API
GeoApiContext geoApiContext = new GeoApiContext.Builder()
.apiKey(apiKey)
.build();
// - Perform the actual request
DirectionsResult directionsResult = DirectionsApi.newRequest(geoApiContext)
.mode(TravelMode.DRIVING)
.origin(origin)
.destination(destination)
.await();
// - Parse the result
DirectionsRoute route = directionsResult.routes[0];
DirectionsLeg leg = route.legs[0];
Duration duration = leg.duration;
return duration.humanReadable;
}
For simplicity, this code does not handle exceptions, error cases (e.g. no route found -> routes.length == 0), nor does it bother with more than one route or leg. Origin and destination could also be set directly as LatLng instances (see DirectionsApiRequest#origin(LatLng) and DirectionsApiRequest#destination(LatLng).
Further reading: android.jlelse.eu - Google Maps Directions API
You can also use
http://maps.google.com/maps?saddr={start_address}&daddr={destination_address}
it will give in direction detail along with distance and time in between two locations
http://maps.google.com/maps?saddr=79.7189,72.3414&daddr=66.45,74.6333&ie=UTF8&0&om=0&output=kml
Calculate Distance:-
float distance;
Location locationA=new Location("A");
locationA.setLatitude(lat);
locationA.setLongitude(lng);
Location locationB = new Location("B");
locationB.setLatitude(lat);
locationB.setLongitude(lng);
distance = locationA.distanceTo(locationB)/1000;
LatLng From = new LatLng(lat,lng);
LatLng To = new LatLng(lat,lng);
Calculate Time:-
int speedIs1KmMinute = 100;
float estimatedDriveTimeInMinutes = distance / speedIs1KmMinute;
Toast.makeText(this,String.valueOf(distance+
"Km"),Toast.LENGTH_SHORT).show();
Toast.makeText(this,String.valueOf(estimatedDriveTimeInMinutes+" Time"),Toast.LENGTH_SHORT).show();
only one question that i did not understand complety is that, on the page of google distance matrix, in the example of :
requesting distance and duration from Vancouver, BC, Canada and from Seattle, WA, USA, to San Francisco, CA, USA and to Victoria, BC, Canada.
in that part, what does it mean BC, WA etc. And in the request
http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR&sensor=false
Why Vancouver+BC and why not Seatle + WA.
And the most important question is that if i want to work with latidute and longitude, not with name of places, how can i do this?
Have a look at the answer i posted on this thread:
android google map finding distance
It uses the Google directions API but you still get the data you require, and uses Lat/Lon for the request. You would also need to add:
JSONObject duration = steps.getJSONObject("duration");
String sDuration = duration.getString("text");
int iDuration = duration.getInt("value");
in order to get the duration, make sure you put it after JSONObject steps = legs.getJSONObject(0);