Search code examples
androidgoogle-mapsgoogle-place-picker

How to implement google map place picker in android after it has been deprecated


i want to use google map place picker in my android app but the Place Picker is deprecated and is not included in the new Places SDK. Please tell me how to implement the latest SDK in my android app

        new LatLng(47.64299816, -122.14351988));
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
builder.setLatLngBounds(latLngBounds);

try {
    startActivityForResult(builder.build(this), PLACE_PICKER_REQ_CODE);
} catch (Exception e) {
    Log.e(TAG, e.getStackTrace().toString());
}

Solution

  • Here is the alternative.

    implementation 'com.google.android.gms:play-services-places:16.1.0'

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
         Places.initialize(getApplicationContext(), getString(R.string.google_maps_key));
    
        PlacesClient placesClient = Places.createClient(this);
    
        mAutocompleteSupportFragment = (AutocompleteSupportFragment) getSupportFragmentManager().findFragmentById(R.id.placeAutocompletefragment); // Fragment is declared in layout file 
    
        mAutocompleteSupportFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG));
    
        mAutocompleteSupportFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(@NonNull Place place) {
    
                Log.d("PlaceAddress", "onPlaceSelected: " + place.getLatLng());
    
                LatLng queriedLocation = place.getLatLng();
                Log.v("Latitude is", "" + queriedLocation.latitude);
                Log.v("Longitude is", "" + queriedLocation.longitude);
    
                mLat = queriedLocation.latitude;
                mLng = queriedLocation.longitude;
    
    
            }
    
            @Override
            public void onError(@NonNull Status status) {
    
            }
        });
    

    here you will get the latlng of selected place.