Using Place Picker in Android Part 2

2 min read

Dear friends

In continuation to my last tutorial

I am here trying to interact with Place picker in Android. For this to work you need to integrate Google Play services in your application.

Lets see what you need to modify in your build.gradle file

dependencies {
….
compile ‘com.google.android.gms:play-services:7.3.0’
….
}

And then you need to make changes in your Java Code. Since I am considering Android Marshmallow so runtime permission way is included in code.

 

package in.relsellglobal.androidnotificationsdemo;

import android.*;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlacePicker;
import com.google.android.gms.maps.model.LatLng;

/**
 * Created by anilkukreti on 19/05/17.
 */

public class PlaceMainActivity extends AppCompatActivity {

    private static final int PLACE_PICKER_REQUEST = 1;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {


            if (isLocationEnabled(PlaceMainActivity.this)) {

                invokePlacePicker();
            } else {
                AlertDialog.Builder dialog = new AlertDialog.Builder(this);
                dialog.setMessage("Please turn on Location").setCancelable(false);
                dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                        Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivityForResult(myIntent, 001);
                    }
                });
                dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                    }
                });
                dialog.show();
            }
        } else {
            // add new m permission logic

            if (isLocationEnabled(PlaceMainActivity.this)) {

                if (ContextCompat.checkSelfPermission(PlaceMainActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(PlaceMainActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, 0);
                } else {

                    invokePlacePicker();

                }

            } else {
                AlertDialog.Builder dialog = new AlertDialog.Builder(this);
                dialog.setMessage("Please turn on Location").setCancelable(false);
                dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                        Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivityForResult(myIntent, 001);
                    }
                });
                dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                    }
                });
                dialog.show();
            }


        }



    }

    public boolean isLocationEnabled(Context mContext) {

        LocationManager lm = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Toast.makeText(PlaceMainActivity.this, "Gps on", Toast.LENGTH_SHORT).show();

            // If location service is enabled then get the current location
            // make changes for targetSDK 23
            return true;
        }
        return false;
    }

    public void invokePlacePicker() {
        PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
        try {
            startActivityForResult(builder.build(PlaceMainActivity.this), PLACE_PICKER_REQUEST);
        } catch (GooglePlayServicesRepairableException e) {
            e.printStackTrace();
        } catch (GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == 0) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
                    && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                invokePlacePicker();
            }
        }
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PLACE_PICKER_REQUEST) {
            if (resultCode == RESULT_OK) {
                Place place = PlacePicker.getPlace(data, this);
                Log.v("TAG","Name = "+place.getName());
                Log.v("TAG","Lat = "+place.getLatLng().latitude);
                Log.v("TAG","Long = "+place.getLatLng().longitude);
                Log.v("TAG",""+place.getAddress());

                Toast.makeText(PlaceMainActivity.this,"Name = "+place.getName()+" "+"Lat = "+place.getLatLng().latitude+" "+"Long = "+place.getLatLng().longitude,Toast.LENGTH_LONG).show();
            }
        }
    }





}

Running code example as an additional module to our Project

is AndroidNotificationsDemo 2

Thanks

Happy Coding.