In this blog, we learn and implement Place Picker in our Application.
Place Picker- The PlacePicker
provides a UI dialog that displays an interactive map and a list of nearby places, including places corresponding to geographical addresses and local businesses. Users can choose a place, and your app can then retrieve the details of the selected place.
So how can we use PlacePicker in our application?
first of all we need to add dependency in application level build.gradle
1 |
compile 'com.google.android.gms:play-services-places:10.2.6' |
add api key in manifest file
1 2 3 |
<meta-data android:name="com.google.android.geo.API_KEY" android:value="your API KEY" /> |
start PlacePicker Intent for getting result in our MainActivity.
1 2 3 4 5 6 7 8 9 10 11 |
public void onClickGetPlace(){ PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder(); try { ((MainActivity)mContaxt).startActivityForResult(builder.build((MainActivity)mContaxt), MainActivity.PLACE_PICKER_REQUEST); } catch (GooglePlayServicesRepairableException e) { e.printStackTrace(); } catch (GooglePlayServicesNotAvailableException e) { e.printStackTrace(); } } |
now get result from PlacePicker Activity.
1 2 3 4 5 6 7 8 |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PLACE_PICKER_REQUEST) { if (resultCode == RESULT_OK) { Place place = PlacePicker.getPlace(data, this); location.setSourceAddress(place.getName().toString()); } } } |