Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get Lat Lang from a place_id returned by autocomplete place api

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?

like image 960
Sajal Avatar asked Sep 19 '14 07:09

Sajal


People also ask

How do I find the latitude and longitude of Google Places autocomplete API?

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();

How do I get lat and long from place id?

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.


3 Answers

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

like image 109
droidmad Avatar answered Oct 13 '22 19:10

droidmad


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.

like image 31
CallMeLaNN Avatar answered Oct 13 '22 20:10

CallMeLaNN


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();
like image 23
rachna patwal Avatar answered Oct 13 '22 20:10

rachna patwal