Skip to content Skip to sidebar Skip to footer

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

Solution 1:

The following code snippet which uses Google Places API for android worked for me

Places.GeoDataApi.getPlaceById(mGoogleApiClient, placeId)
    .setResultCallback(newResultCallback<PlaceBuffer>() {
  @OverridepublicvoidonResult(PlaceBuffer places) {
    if (places.getStatus().isSuccess()) {
      finalPlacemyPlace= places.get(0);
      LatLngqueriedLocation= 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

Solution 2:

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.

Solution 3:

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

LatLngdestinationLatLng= place.getLatLng();

and can see through toast

 destlat = destinationLatLng.latitude;
 destLon = destinationLatLng.longitude;
 Toast.makeText(getApplicationContext(), "" + destlat +','+ destLon, Toast.LENGTH_LONG).show();

Solution 4:

Each place returned in the place-autocomplete response has an Id and a reference string as explained here.

Use either (preferably Id since reference is deprecated) to query Places API for the full information about that place (including lat/lng): https://developers.google.com/places/documentation/details#PlaceDetailsRequests

Regarding shyam's comment - Geocoding will work only if you got a full address in the autocomplete response which is not always the case. Also Geocoding gives a list of possible results since the place description you get in the autocomplete response is not unique. Depending on your needs, geocoding might be enough.

Solution 5:

Based on the latest version of AutoComplete documentation

Option 1: Embed an AutocompleteSupportFragment

AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
            getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);

 autocompleteFragment
    .setPlaceFields(Arrays.asList(Place.Field.ID, 
    Place.Field.NAME,Place.Field.LAT_LNG,Place.Field.ADDRESS));

Option 2: Use an intent to launch the autocomplete activity

List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME,Place.Field.LAT_LNG,Place.Field.ADDRESS);

// Start the autocomplete intent.Intentintent=newAutocomplete.IntentBuilder(
        AutocompleteActivityMode.FULLSCREEN, fields)
        .build(this);
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);

whichever fields you are interested in, you have to mention as mentioned above.

You'll get result as below:

onPlaceSelected: 

{

"a":"#90, 1st Floor, Balaji Complex, Kuvempu Main Road, Kempapura, Hebbal 
    Kempapura, Bengaluru, Karnataka 560024, India",
"b":[],
"c":"ChIJzxEsY4QXrjsRQiF5LWRnVoc",
"d":{"latitude":13.0498176,"longitude":77.600347},
    "e":"CRAWLINK Networks Pvt. Ltd."
}

Note: The result displayed is by parsing the Place object to json

Post a Comment for "Get Lat Lang From A Place_id Returned By Autocomplete Place Api"