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);
Related
I am implementing the search functionality on the google maps. For this I am using the Places.GeoDataApi of google.
But the problem I am facing that it is searching based on the LatLanBounds value.
Currently I am passing sydney specific bounds in below api,so at the time of search its biased towards these bounds and checking syndey specific locations first.
private static final LatLngBounds mBounds= new LatLngBounds(
new LatLng(-34.041458, 150.790100), new LatLng(-33.682247, 151.383362));
PendingResult<AutocompletePredictionBuffer> results =
Places.GeoDataApi
.getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
mBounds, mPlaceFilter);
As per my requirement, Above API should return the result of near by area first. For example, if user is in India and search for something it should return the suggestions related to India on priority and If user in Brazil try to search something API should return the suggestion related to Brazil. I do not know how to pass the dynamic LatLng bounds values based on user current country or location.
your help will be really appreciated.
This is month old question, but I hope my answer will help the OP or somebody else with similar problems.
The answer for main question is pretty straighforward:
Create new LatLngBounds (docs) object each time you get information about user location and pass this object as a parameter of .getAutocompletePredictions:
LatLng sw = new LatLng(SOUTH_WEST_LAT, SOUTH_WEST_LNG);
LatLng ne = new LatLng(NORTH_EAST_LAT, NORTH_EAST_LNG);
LatLngBounds bounds = new LatLngBounds(sw, ne);
Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, constraint, bounds, null);
These two points, south-west and north-east are opposite corners of the square that makes an area around user's location. This is the bound.
About your second question, in the comment. I suppose you have your user location (eg. from GPS), for example in Brazil, and you need to find the bounds of this location.
To do that, I would use basic Google Places API call (you can try it out in your browser):
https://maps.googleapis.com/maps/api/geocode/json?address=Brazil
that will return you JSON formatted data about Brazil with field bounds in it:
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 42.1282269,
"lng" : -87.7108162
},
"southwest" : {
"lat" : 42.0886089,
"lng" : -87.7708629
}
},
...
}
As you can see, these are the north-east and south-west bounds of square in which Brazil lies. You can put them into the LatLngBounds object now.
Hypothetical question. I'm building an augmented reality app using Google Maps API for Android. I'm wondering if there's any data that I can use to determine whether a building lies between me and a specified location. I ask this because when sufficiently zoomed-in, there is clearly 3D data on the shape of buildings included on the map. I was wondering if there was a method like:
boolean buildingInTheWay(myLocation, destinationLocation);
if (buildingInTheWay) {
//Do something
} else {
//Do something else
}
Perhaps there's also something that could be done where if the route to a location is much longer than the birds-eye path to a location, there must be an obstacle in the way (imagine two parallel streets like so:
- = street
X = buildings
A = start location
B = destination location
---------C----A-------
xxxxxxxx | xxxxxxxxx
---------D----B-------
Here, A to B would return true, as the route around the buildings is a lot longer than the direct distance. But C to D would return false, as the route following a road is almost exactly the same distance.
However, that's not very accurate - what about between buildings? I wonder if each building on Google Maps has lat/lng points for each of its corners?
Any thoughts, anyone?
The code snippet below works from most lat/lon coordinates.
However, passing specific coordinates returns no results.
Geocoder geocoder = new Geocoder(HappyApp.instance(), Locale.getDefault());
Log.w(TAG, "geocoder object created and is present: " + Geocoder.isPresent());
List<Address> addresses = geocoder.getFromLocation(32.0923805, 35.165159, 1);
Log.w(TAG, "List size: " + addresses.size());
Geocoder.isPresent() returns true.
List size is 0.
The location can be confirmaed by typing Ari'el in Google maps.
Same for Gaza (just Gaza not strip).
My question is it an issue with Android API/Google maps or am I doing something wrong?
Thanks.
It seems that Google avoid providing Country and other data where the territory is under controversy dispute like north Cyprus and the Gaza strip.
Whenever we get 0 results, we use the following online service: http://www.geonames.org
Google maps geocoder is not available in all regions of the world , period.
If the same app will work for a different region except for a particular one (esp Africa and Middle East), its just because that area isnt really covered by geocoder.
:(
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.
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();