Google Place Picker Android Studio

15,088

Add the dependency to yourbuild.gradle (replace <version-number> with the version you want, which you can find, for example, at Gradle, Please):

compile "com.google.android.gms:play-services-places:<version-number>"

Add your key to your AndroidManifest.xml:

<meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/map_api_key"/>

Your Activity/Fragment needs these attributes and methods:

private final static int PLACE_PICKER_REQUEST = 999;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    checkPermissionOnActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        switch (requestCode){
        case PLACE_PICKER_REQUEST:
            Place place = PlacePicker.getPlace(this, data);
            String placeName = String.format("Place: %s", place.getName());
            double latitude = place.getLatLng().latitude;
            double longitude = place.getLatLng().longitude;

        }
    }
}

finally, this is the code to open a PlacePicker

PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
     // for activty
     startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
     // for fragment         
     //startActivityForResult(builder.build(getActivity()), PLACE_PICKER_REQUEST); 
} catch (GooglePlayServicesRepairableException e) {
     e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
     e.printStackTrace();
}
    
    
Share:
15,088
Admin
Author by

Admin

Updated on June 07, 2022

Comments

  • Admin
    Admin almost 2 years

    Does anyone know how to implement Google Place Picker API in Android Studio? Tried tutorials, unsuccessful. I want a working AutoComplete Text Field that has places show up and when you click on the place it will display in the Google Map Fragment.