Android

Place Picker Widget – Android

Place picker widget or place picker api

Today i will introduce you a widget which is very helpful for your app.

If you want to build an app where users can find its all available nearest place So then this tutorial is for you.

 

  • First you need a API key

Then add permission

  • Then add gradle dependency

compile “com.google.android.gms:play-services:8.1.0”

 

  • Then code

 

For API key:

Search google for ‘google console api key’ or see tutorial ‘how to create a key for google map’

 

For Permission:

 

<permission
android:name=“com.example.shaon.googlemap.permission.MAPS_RECEIVE”
android:protectionLevel=“signature” />

<!–It helps to use call phone tower and wireless hub –>
<uses-permission android:name=“android.permission.ACCESS_COARSE_LOCATION” />

<uses-permission android:name=“android.permission.INTERNET” />
<!– It helps to use gps but it consumes much battery power–>
<uses-permission android:name=“android.permission.ACCESS_FINE_LOCATION” />
<uses-permission android:name=“android.permission.WRITE_EXTERNAL_STORAGE” />
<uses-permission android:name=“com.example.shaon.googlemap.permission.MAPS_RECEIVE” />
<uses-permission android:name=“com.google.android.providers.gsf.permission.READ_GSERVICES” />

 

 

 

Set your API key in manifest

 

Set it before application finish tag and after an activity finish tag

 

</activity>

<meta-data
android:name=“com.google.android.geo.API_KEY”
android:value=“AIzaSyDo-PyCl4kghDcl5aYxHhSIiJuffKi3e3E” />

</application>

 

 

Coding site:

 

First create an activity called MainActivity.class

Then add this below code

 

 

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
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 Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{

public GoogleApiClient mClient;
public static final int PLACE_PICKER_REQUEST = 1000;

TextView mViewAttributions, mViewName, mViewAddress;
Button btnPick;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mViewAddress = (TextView) findViewById(R.id.mViewAddress);
mViewAttributions = (TextView) findViewById(R.id.mViewAttributions);
mViewName = (TextView) findViewById(R.id.mViewName);

mClient = new GoogleApiClient.Builder(this).addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API).build();

btnPick = (Button) findViewById(R.id.pick);

btnPick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();

try {
startActivityForResult(builder.build(MainActivity.this), PLACE_PICKER_REQUEST);
} catch (Exception e) {

}
}
});

}

@Override
protected void onStart() {
super.onStart();

mClient.connect();
}

@Override
protected void onResume() {
super.onResume();

mClient.reconnect();
}

@Override
protected void onStop() {
mClient.disconnect();

super.onStop();

}

@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {

if (requestCode == PLACE_PICKER_REQUEST
&& resultCode == Activity.RESULT_OK) {

// The user has selected a place. Extract the name and address.
final Place place = PlacePicker.getPlace(data, this);

final CharSequence name = place.getName();
final CharSequence address = place.getAddress();
String attributions = PlacePicker.getAttributions(data);
if (attributions == null) {
attributions = “”;
}

mViewName.setText(name);
mViewAddress.setText(address);
mViewAttributions.setText(Html.fromHtml(attributions));

} else {
super.onActivityResult(requestCode, resultCode, data);
}
}

@Override
public void onConnected(Bundle bundle) {

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}
}

 

 

add this below code into your activity_main.xml file

 

<?xml version=“1.0” encoding=“utf-8”?>
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:orientation=“vertical” android:layout_width=“match_parent”
android:layout_height=“match_parent”>

<Button
android:id=“@+id/pick”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“Get place”/>

<TextView
android:id=“@+id/mViewName”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“”/>

<TextView
android:id=“@+id/mViewAddress”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“”/>

<TextView
android:id=“@+id/mViewAttributions”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“”/>

</LinearLayout>

 

 

If you have any query comment below

 

 

Leave a Reply

Your email address will not be published.

one × 2 =