Place search is used to search place by entering keyword in search field, then this provides the suggested list of place and choose any one from number of suggested place.
Steps to use place search 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.placesearch.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.placesearch.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:gravity="center_horizontal"
android:orientation="vertical"
tools:context="com.pankaj.placesearch.demo.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Search a place"
android:textColor="@color/colorPrimary"
android:textStyle="bold" />
<!--add fragment to search place-->
<fragment
android:id="@+id/place_autocomplete_fragment"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout> - Open your MainActivity.JAVA class and update it -
package com.pankaj.placesearch.demo;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.location.places.ui.PlaceAutocompleteFragment;
import com.google.android.gms.location.places.ui.PlaceSelectionListener;
public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {
//_______create object
private GoogleApiClient mGoogleApiClient;
private final int PLACE_AUTOCOMPLETE_REQUEST_CODE = 11;
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();
//________initialize auto complete place
autocompletePlace();
}
private void autocompletePlace() {
//______________create object of PlaceAutocompleteFragment.
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
//______________add listener to PlaceAutocompleteFragment.
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
//______________Method will be auto call on selection of any place.
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
//______________format place name.
String toastMsgAuto = String.format("Place: %s", place.getName());
//______________show selected place in massage.
Toast.makeText(MainActivity.this, toastMsgAuto, Toast.LENGTH_LONG).show();
}
//______________Method will be auto call, if error occur on selection of any place.
@Override
public void onError(Status status) {
// TODO: Handle the error.
Log.i(TAG, "An error occurred: " + status);
}
});
}
//______________Method will be auto call, if connection fail from google server.
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(this, "Connection failed", Toast.LENGTH_SHORT).show();
}
} - Now all the development steps for Place Search has completed, Please run the application.
- Enter some text in search field, this will show the listed suggestions . See the result on screen-
- Select any place from this suggested list. See the result on screen-
- Good bye, Thanks to read this blog.








