Learn About Android Place Picker API

Place Picker API
 

Introduction

 
In this article, we will learn about one of the greatest APIs for Android provided by Google, called Place Picker API. This API is used to Pick a Place from Google Maps Application without any Map Integration.
 
The Place Picker 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.
 

Place Picker API

 
The Place Picker 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. To know more, click here.
 
Steps
 
I have divided this Implementation into 4 steps as shown in the following.
  • Step 1: Creating a New Project with Android Studio
  • Step 2: Enabling the API in the Google Console.
  • Step 3: Setting up the library and AndroidManifest for the project.
  • Step 4: Implementation of the Place Picker API.
Step 1 - Creating a New Project with Android Studio
  1. Open Android Studio and select "Create new project".
  2. Name the project as per your wish and select your activity template.
     
    Place Picker API
     
  3. Click the Finish button to create the new project in Android Studio.
Step 2 - Enabling the API in the Google Developer Console
  1. For using Google Places API, We need to know SHA1 Key, which is used in the Google Developer Console. You can get your SHA1 Key using Command Prompt. 
    1. keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android  
  1. Open Google Developer Console
  2. Create a New Project or you can use your Existing Project.
  3. Go to the Dashboard and click Enable API.
  4. Click Google Places API for Android and Select Enable
  5. Create a new API Key, which is used later.
Step 3 - Setting up the library and AndroidManifest for the project
  1. Add Google Play service library dependency in your app level build.gradle file. Here, I used the following dependency. You can change as per your Android SDK. 
    1. compile 'com.google.android.gms:play-services:9.2.0'  
  1. Then click “Sync Now” to add the library.
  2. Now open your Manifest File (AndroidManifest.xml) and the following permission. 
    1. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
    2. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
    3. <uses-permission android:name="android.permission.INTERNET" />  
  1. Add the following meta data to apply the API KEY. 
    1. <meta-data  
    2.     android:name="com.google.android.geo.API_KEY"  
    3.     android:value="Your_API_Key"/>  
Step 4 - Implementation of Place Picker API
  1. Add the following to initialize the API. 
    1. //Declaration of Google API Client  
    2. private GoogleApiClient mGoogleApiClient;  
    3. private int PLACE_PICKER_REQUEST = 1;  
  1. In the onCreate method of the Activity initialize the GoogleApiClient. 
    1. mGoogleApiClient = new GoogleApiClient  
    2.                 .Builder(this)  
    3.                 .addApi(Places.GEO_DATA_API)  
    4.                 .addApi(Places.PLACE_DETECTION_API)  
    5.                 .enableAutoManage(thisthis)  
    6.                 .build();  
  1. Implement the onStart and onStop method and do the client connection task. 
    1. @Override  
    2. protected void onStart() {  
    3.     super.onStart();  
    4.     mGoogleApiClient.connect();  
    5. }  
    6.   
    7. @Override  
    8. protected void onStop() {  
    9.     mGoogleApiClient.disconnect();  
    10.     super.onStop();  
    11. }  
  1. Start Place Picker Intent using the following snippet. 
    1. PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();  
    2. try {  
    3.     startActivityForResult(builder.build(MainActivity.this), PLACE_PICKER_REQUEST);  
    4. catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {  
    5.     e.printStackTrace();  
    6. }  
    After this, you will get Google Maps Screen like below. It has 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.
  1. The Details can be retrieved in the onActivityResult method of your Activity. 
    1. @Override  
    2. protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    3.     if (requestCode == PLACE_PICKER_REQUEST) {  
    4.         if (resultCode == RESULT_OK) {  
    5.             Place place = PlacePicker.getPlace(data, this);  
    6.             StringBuilder stBuilder = new StringBuilder();  
    7.             String placename = String.format("%s", place.getName());  
    8.             String latitude = String.valueOf(place.getLatLng().latitude);  
    9.             String longitude = String.valueOf(place.getLatLng().longitude);  
    10.             String address = String.format("%s", place.getAddress());  
    11.             stBuilder.append("Name: ");  
    12.             stBuilder.append(placename);  
    13.             stBuilder.append("\n");  
    14.             stBuilder.append("Latitude: ");  
    15.             stBuilder.append(latitude);  
    16.             stBuilder.append("\n");  
    17.             stBuilder.append("Logitude: ");  
    18.             stBuilder.append(longitude);  
    19.             stBuilder.append("\n");  
    20.             stBuilder.append("Address: ");  
    21.             stBuilder.append(address);  
    22.             tvPlaceDetails.setText(stBuilder.toString());  
    23.         }  
    24.     }  
Demo
 
The following screenshots demonstrate the usage of Place Picker API for Android.
 
 Place Picker API Figure 1: Place Picker Intent Builder Output
 Place Picker API Figure 2: Place Picker Dialog (after selecting the place)
 Place Picker API Figure 3: Output of the Intent Builder from onActivityResult.
 
Full code of MainActivity.java
  1. public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {  
  2.   
  3.     private GoogleApiClient mGoogleApiClient;  
  4.     private int PLACE_PICKER_REQUEST = 1;  
  5.     private TextView tvPlaceDetails;  
  6.     private FloatingActionButton fabPickPlace;  
  7.   
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.activity_main);  
  12.   
  13.         initViews();  
  14.   
  15.         mGoogleApiClient = new GoogleApiClient  
  16.                 .Builder(this)  
  17.                 .addApi(Places.GEO_DATA_API)  
  18.                 .addApi(Places.PLACE_DETECTION_API)  
  19.                 .enableAutoManage(thisthis)  
  20.                 .build();  
  21.   
  22.   
  23.         fabPickPlace.setOnClickListener(new View.OnClickListener() {  
  24.             @Override  
  25.             public void onClick(View view) {  
  26.                 PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();  
  27.                 try {  
  28.                     startActivityForResult(builder.build(MainActivity.this), PLACE_PICKER_REQUEST);  
  29.                 } catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {  
  30.                     e.printStackTrace();  
  31.                 }  
  32.             }  
  33.         });  
  34.     }  
  35.   
  36.     private void initViews() {  
  37.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);  
  38.         setSupportActionBar(toolbar);  
  39.         fabPickPlace = (FloatingActionButton) findViewById(R.id.fab);  
  40.         tvPlaceDetails = (TextView) findViewById(R.id.placeDetails);  
  41.     }  
  42.   
  43.     @Override  
  44.     protected void onStart() {  
  45.         super.onStart();  
  46.         mGoogleApiClient.connect();  
  47.     }  
  48.   
  49.     @Override  
  50.     protected void onStop() {  
  51.         mGoogleApiClient.disconnect();  
  52.         super.onStop();  
  53.     }  
  54.   
  55.     @Override  
  56.     public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {  
  57.         Snackbar.make(fabPickPlace, connectionResult.getErrorMessage() + "", Snackbar.LENGTH_LONG).show();  
  58.     }  
  59.   
  60.     @Override  
  61.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  62.         if (requestCode == PLACE_PICKER_REQUEST) {  
  63.             if (resultCode == RESULT_OK) {  
  64.                 Place place = PlacePicker.getPlace(data, this);  
  65.                 StringBuilder stBuilder = new StringBuilder();  
  66.                 String placename = String.format("%s", place.getName());  
  67.                 String latitude = String.valueOf(place.getLatLng().latitude);  
  68.                 String longitude = String.valueOf(place.getLatLng().longitude);  
  69.                 String address = String.format("%s", place.getAddress());  
  70.                 stBuilder.append("Name: ");  
  71.                 stBuilder.append(placename);  
  72.                 stBuilder.append("\n");  
  73.                 stBuilder.append("Latitude: ");  
  74.                 stBuilder.append(latitude);  
  75.                 stBuilder.append("\n");  
  76.                 stBuilder.append("Logitude: ");  
  77.                 stBuilder.append(longitude);  
  78.                 stBuilder.append("\n");  
  79.                 stBuilder.append("Address: ");  
  80.                 stBuilder.append(address);  
  81.                 tvPlaceDetails.setText(stBuilder.toString());  
  82.             }  
  83.         }  
  84.     }  
  85. }  
Download Code
 
You can download the full source code of the article in GitHub. If you like this article, do star the repo in GitHub. 


Similar Articles