Reputation: 5256
I am not able to launch Google Place Picker from my fragment. Followed all the steps and forums for the root cause but not able to resolve my issue.
Links Google Place Picker
Already checked following without any success
Android Place Picker closes immediately after launch
Place Picker Automatically close after launch
I have created Google Places API for Android key and added to manifest
I have even tried a release build.
Only error message I receive is
03-18 12:02:32.524 1679-1900/? E/GCoreFlp: Location requests inside Google
Play services must contain a tag to aid in debugging. Use LocationRequestInternal.create to wrap your LocationRequest, and pass it to requestLocationUpdates.
03-18 12:02:32.527 29916-29916/? E/PlacePicker: Place Picker closing due to ERROR
Need Help
My Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tyagiabhinav.test">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="KEY-REMOVED-ON-STACKOVERFLOW" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<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>
</application>
</manifest>
My Gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.tyagiabhinav.test"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.google.android.gms:play-services-location:8.4.0'
}
My Fragment Code
package com.tyagiabhinav.test;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
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.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.location.places.ui.PlacePicker;
/**
* Created by abhinavtyagi on 18/03/16.
*/
public class MainFragment extends Fragment implements ConnectionCallbacks, OnConnectionFailedListener {
private View rootView;
private GoogleApiClient mGoogleApiClient;
private boolean isLocationServiceConnected = false;
private static final String LOG_TAG = MainFragment.class.getSimpleName();
private static final int PLACE_PICKER_REQUEST = 7;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.main_fragment, container, false);
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.build();
}
Button btn = (Button) rootView.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(LOG_TAG, "Clicked");
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
Log.d(LOG_TAG, "Ask for Permission");
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, PLACE_PICKER_REQUEST);
} else {
// No explanation needed, we can request the permission.
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, PLACE_PICKER_REQUEST);
}
} else {
Log.d(LOG_TAG, "Permission Available");
placePicker();
}
}
});
return rootView;
}
private void placePicker() {
if (isLocationServiceConnected) {
Log.d(LOG_TAG, "Connected to location service");
PlacePicker.IntentBuilder intentBuilder = new PlacePicker.IntentBuilder();
try {
Intent intent = intentBuilder.build(getActivity());
startActivityForResult(intent, PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
} else {
Log.d(LOG_TAG, "Not connected to location service");
Toast.makeText(getActivity(), "Not connected to location service", Toast.LENGTH_LONG).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
Log.d(LOG_TAG, "onRequestPermissionsResult");
switch (requestCode) {
case PLACE_PICKER_REQUEST: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(LOG_TAG, "Permission Granted");
// permission was granted, yay! Do the
// contacts-related task you need to do.
placePicker();
} else {
Log.d(LOG_TAG, "Permission Denied");
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(getActivity(), "Location Permission is required for accessing Place Picker!", Toast.LENGTH_LONG).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
@Override
public void onStart() {
super.onStart();
if (mGoogleApiClient != null)
mGoogleApiClient.connect();
}
@Override
public void onStop() {
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
super.onStop();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(LOG_TAG, "Location Connection Failed");
isLocationServiceConnected = false;
}
@Override
public void onConnected(Bundle bundle) {
Log.d(LOG_TAG, "Location Service Connected");
isLocationServiceConnected = true;
}
@Override
public void onConnectionSuspended(int i) {
Log.d(LOG_TAG, "Location Service Suspended");
isLocationServiceConnected = false;
}
}
Upvotes: 3
Views: 4066
Reputation: 5256
Was putting meta-data tag wrongly
corrected manifest file :P
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tyagiabhinav.test">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="KEY-REMOVED-ON-STACKOVERFLOW" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Upvotes: 2