Ad

Converting A PlaceID String To LatLng Object

- 1 answer

Im trying to convert a placeId String which I stored on a database to LatLng coordinates, I got the id like so: place.getId() from a place object and now when I retirived it from the server I would like to convert it back into coordinates or at least a Place object.

Thank you!

Ad

Answer

Declare the object as

GoogleApiClient mGoogleApiClient;

Then initialise it as

 mGoogleApiClient =
            new GoogleApiClient.Builder(...)
            ...
            .build();

And pass it to the Places API's function.

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

And retrieve the coordinates in the callback. (code copied from here)

Ad
source: stackoverflow.com
Ad