Google Places API for Android Place Picker Does no

2019-01-20 08:43发布

问题:

I am unable to use the Place picker in my app. The API worked then just stopped one day with no explanation.The picker shows up for a very short period of time then simply closes with no exception and nothing in the logs. The onActivityResult function is called with requestCode=1, resultCode=2(error code) and with no data. The sample code from google does the same thing. I have tried everything to get this working. anybody experience anything similar? Help please.

AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.easy.parcelapp."
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="22" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.RECORD_VIDEO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.BROADCAST_STICKY" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"  />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

    <application
        android:name=".EasiParcelApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyDEcV-My-Actual-API-Key-goes-here" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Holo.Light.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".RegistrationActivity"
            android:label="@string/title_activity_register"
            android:parentActivityName=".MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.easiparcel.MainActivity" />
        </activity>
        <activity
            android:name=".JobListingActivity"
            android:label="@string/title_activity_listing" >
        </activity>
        <activity
            android:name=".NewJobActivity"
            android:label="@string/title_activity_new_job"
            android:parentActivityName=".JobListingActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.easiparcel.JobListingActivity" />
        </activity>
        <activity
            android:name=".NewJobConfirmation"
            android:label="@string/title_activity_new_job_confirmation"
            android:parentActivityName=".NewJobActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.easiparcel.NewJobActivity" />
        </activity>
        <activity
            android:name=".JobDetails"
            android:label="@string/title_activity_job_details"
            android:parentActivityName=".JobListingActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.easiparcel.JobListingActivity" />
        </activity>
    </application>

</manifest>

Activity.java

  public void HandleSenderAddressPickerClicked(View view)
{
    int PLACE_PICKER_REQUEST = 1;
    LoadPlacePicker(PLACE_PICKER_REQUEST);

}

public void HandleRecieverAddressPickerClicked(View view)
{
    int PLACE_PICKER_REQUEST = 2;
    LoadPlacePicker(PLACE_PICKER_REQUEST);

}

private void LoadPlacePicker(int PlacePickerRequest)
{
    try {

        PlacePicker.IntentBuilder intentBuilder = new PlacePicker.IntentBuilder();
        Intent intent = intentBuilder.build(NewJobActivity.this);
        // Start the Intent by requesting a result, identified by a request code.
        startActivityForResult(intent, PlacePickerRequest);


    } catch (GooglePlayServicesRepairableException e) {
        GooglePlayServicesUtil
                .getErrorDialog(e.getConnectionStatusCode(), NewJobActivity.this, 0);
    } catch (GooglePlayServicesNotAvailableException e) {
        Toast.makeText(NewJobActivity.this, "Google Play Services is not available.",
                Toast.LENGTH_LONG)
                .show();
    }
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            Place place = PlacePicker.getPlace(data, this);
            String toastMsg = String.format("Sender Place: %s", place.getName());
            Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
        }
    }

    //Reciever
    if (requestCode == 2) {
        if (resultCode == RESULT_OK) {
            Place place = PlacePicker.getPlace(data, this);
            String toastMsg = String.format("Receiver Place: %s", place.getName());
            Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
        }
    }
}

回答1:

I figured out the answer to my own question.

If anybody else encounters the same issue, make sure you enable "Places API for Android" and not just "Places API" in the Developer Console. "Places API for Android" will not show up under "APIs & Auth/APIs" because it isnt a popular API (yet). You will have to search for it using the API search box.



回答2:

instead of this:

<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="your_key"/>

add this:

<meta-data            
    android:name="com.google.android.geo.API_KEY"
    android:value="your_key" />

It worked for me!!!



回答3:

Check your build variant.

If you switched back from release to debug, you typically also get result code 2.

Switching back to release did the trick for me.