Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Place Picker Android Studio

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.

like image 243
Justin Luna Avatar asked Mar 30 '17 05:03

Justin Luna


Video Answer


1 Answers

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();
}
    
    
like image 126
Jd Prajapati Avatar answered Nov 05 '22 20:11

Jd Prajapati