get Lat Lang from a place_id returned by autocomplete place api

前端 未结 12 1452
情深已故
情深已故 2021-02-02 05:58

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 a

相关标签:
12条回答
  • 2021-02-02 06:06

    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.

    0 讨论(0)
  • 2021-02-02 06:06

    Geocoding is a very indirect solution, and like the second responder said, if you do "Apple Store" it may not return the full address. Instead:

    Place_ID contains everything you need. I am assuming you know how to get the place_id from the Places API (they have a full example if not).

    Then pull a second request for the place details (including latitude and longitude under the geometry section) using the place_id following this documentation: https://developers.google.com/places/documentation/details?utm_source=welovemapsdevelopers&utm_campaign=mdr-devdocs

    0 讨论(0)
  • 2021-02-02 06:08

    I got very simple solution to this problem thanks answer by Muhammad Yasir.

        List<Place.Field> placeFields = Arrays.asList(Place.Field.LAT_LNG, Place.Field.ADDRESS_COMPONENTS);
        autocompleteFragment.setPlaceFields(placeFields);
    
        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                LatLng latLng = place.getLatLng(); // This will have required latitude and longitude
             }
       }
    

    You can specify any field you want as in the list like name, address etc.

    Note: This answer is for android java but I think there will be similar approach in other languages like javascript.

    0 讨论(0)
  • 2021-02-02 06:09
    Places.GeoDataApi.getPlaceById(mGoogleApiClient, placeId)
      .setResultCallback(new ResultCallback < PlaceBuffer > () {
        @Override
        public void onResult(PlaceBuffer places) {
          if (places.getStatus().isSuccess() && places.getCount() > 0) {
            final Place myPlace = places.get(0);
            Log.i(TAG, "Place found: " + myPlace.getName());
            LatLng latlangObj = myPlace.getLatLng();
            Log.v("latitude:", "" + latlangObj.latitude);
            Log.v("longitude:", "" + latlangObj.longitude);
          } else {
            Log.e(TAG, "Place not found");
          }
          places.release();
        }
      });
    

    Use this method for getting lat and lang from placeid.

    0 讨论(0)
  • 2021-02-02 06:16

    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

    0 讨论(0)
  • 2021-02-02 06:16

    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();
    
    0 讨论(0)
提交回复
热议问题