android - Place Picker 在启动后关闭

标签 android google-maps-api-3 geolocation location google-places-api

我无法从我的 fragment 中启动 Google Place Picker。 按照根本原因的所有步骤和论坛进行操作,但无法解决我的问题。

链接 Google Place Picker
已经检查过以下但没有成功
Android Place Picker closes immediately after launch
Place Picker Automatically close after launch

我已经为 Android key 创建了 Google Places API 并添加到 list

我什至尝试过发布版本。

我收到的唯一错误信息是

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

需要帮助

我的 list 文件

<?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>

我的 Gradle 文件

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'
}

我的 fragment 代码

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;
    }


}

最佳答案

错误地放置了元数据标签

更正 list 文件 :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>

关于android - Place Picker 在启动后关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36077696/

相关文章:

java - 谷歌地图查询地址字符串格式而不是纬度/经度?

javascript - google maps api 地理定位和地点搜索

java - 如何在BroadcastReceiver服务中获取纬度和经度值?

java - 无法使用简单适配器查看数据 Sqlite

java - 我如何从 Android SDK 解析 PHP 数组?

javascript - Google map API 扭曲标记

ios - React Native Expo 应用程序在发布到 Testflight 时崩溃

c# - 如何在 TCPListener 上阻止来自欧洲的所有连接

android - android中的文档可视化?

android - 如何清除 Android 设备上缓存的 Playready 许可证?