Place Picker is use to select single place from number of place choice on map.
Steps to use place Picker in your project.
- Crate a project in android studio.
- Open your build.gradle(app) and add a dependency to it-
//______add google play service
compile 'com.google.android.gms:play-services:9.0.0'apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.pankaj.placepicker.demo"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.1'
testCompile 'junit:junit:4.12'
//______add google play service
compile 'com.google.android.gms:play-services:9.0.0'
} - Open your menifest.xml and update it-
<!--mata data for place api-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/app_id" /><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pankaj.placepicker.demo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--mata data for place api-->
<!--https://console.developers.google.com/apis-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/app_id" />
</application>
</manifest> - Look on the meta data i used "app_id". Go to the google console and create your app.
- Enter the application name and click on "create".
- Select the dashboard from the left side menu and click on Enable API.
- Now search "Google Places API" and select from the filtered list.
- Select "Credentials" from left side menu. and click on "Create Credentials"
Click on "API Key" - Now Finally you got api key, copy this and add into the string.xml.
- Open your main_activity.xml and update it-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.pankaj.placepicker.demo.MainActivity">
<!--button to open place picker-->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:onClick="openPlacePicker"
android:text="Pick Address By Place Picker" />
</LinearLayout> - Open your MainActivity.JAVA class and update it -
package com.pankaj.placepicker.demo;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.location.places.ui.PlacePicker;
public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {
//_______create object
private GoogleApiClient mGoogleApiClient;
private final int PLACE_PICKER_REQUEST = 1;
private String TAG = "place";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//________initialize google client api
mGoogleApiClient = new GoogleApiClient
.Builder(this)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.enableAutoManage(this, this)
.build();
}
//_________methode will be call when user click on "Pick Address By Place Picker"
public void openPlacePicker(View view) {
//___________create object of placepicker builder
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
//__________start placepicker activity for result
startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(this, "Connection failed", Toast.LENGTH_SHORT).show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//____________check for successfull result
if (resultCode == RESULT_OK) {
switch (requestCode) {
//_________case for placepicker
case PLACE_PICKER_REQUEST:
//______create place object from the received intent.
Place place = PlacePicker.getPlace(data, this);
//______get place name from place object
String toastMsg = String.format("Place: %s", place.getName());
//_________show toast message for selected place
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
break;
}
}
}
} - Now all the development steps for Place Picker has completed, Please run the application.
- Click on "Pick Address By Place Picker", this will be open the places screen you can select any place by moving on the map, and select any place. See the result on screen-
- If you want to search place then click on the search bar and type keyword. Here auto suggestion will be listed so you select any place from this suggested list. See the result on screen-
- When you will select any place a popup dialog will be shown for the confirmation then click on "OK". See the result on screen-
- Good bye, Thanks to read this blog.










