I am searching the place in my apps using the google autocomplete place api and now I want to get latitude and longitude of the place that i have searched. How to get latitude and longitude from the result returned by autocomplete place api by google in android?
Call Google API with address for lat and long In this step, you will create a javascript code for calling the google geocode v3 api with the address to get latitude and longitude from address. var place = autocomplete. getPlace();
From the place_id you got, query Place Details something like https://maps.googleapis.com/maps/api/place/details/json?placeid={placeid}&key={key} and you can get the lat and lng from result. geometry.
The following code snippet which uses Google Places API for android worked for me
Places.GeoDataApi.getPlaceById(mGoogleApiClient, placeId)
.setResultCallback(new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(PlaceBuffer places) {
if (places.getStatus().isSuccess()) {
final Place myPlace = places.get(0);
LatLng queriedLocation = myPlace.getLatLng();
Log.v("Latitude is", "" + queriedLocation.latitude);
Log.v("Longitude is", "" + queriedLocation.longitude);
}
places.release();
}
});
Visit Google Places API for Android for a full list of methods to retrieve data from a place
Google Place Details is the answer.
From the place_id
you got, query Place Details something like https://maps.googleapis.com/maps/api/place/details/json?placeid={placeid}&key={key}
and you can get the lat
and lng
from result.geometry.location
JSON.
do provide Place.Field.LAT_LNG to get the latitude and longitude for the place.
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID,
Place.Field.NAME,Place.Field.LAT_LNG));
then get LatLng
LatLng destinationLatLng = place.getLatLng();
and can see through toast
destlat = destinationLatLng.latitude;
destLon = destinationLatLng.longitude;
Toast.makeText(getApplicationContext(), "" + destlat + ',' + destLon, Toast.LENGTH_LONG).show();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With