Google Places API for Android地方选择器不起作用

时间:2015-04-20 04:20:54

标签: android google-places-api

我无法在我的应用中使用地方选择器。 API工作然后停止了一天没有任何解释。选择器显示很短的时间然后只是关闭,没有例外,日志中没有任何内容。使用requestCode = 1,resultCode = 2(错误代码)并且没有数据调用onActivityResult函数。来自谷歌的示例代码做同样的事情。我已经尝试了一切来使这个工作。有人经历过类似的事吗?请帮助。

的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();
        }
    }
}

3 个答案:

答案 0 :(得分:27)

我找到了自己问题的答案。

如果其他人遇到同样的问题,请务必在Developer Console中启用“Places API for Android”,而不仅仅是“Places API”“Places API for Android”不会显示在“API&amp; Auth / API”下,因为它还不是一个受欢迎的API。您必须使用API​​搜索框搜索它。

答案 1 :(得分:10)

而不是:

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

添加:

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

它对我有用!!!

答案 2 :(得分:0)

检查您的构建版本。

如果您从release切换回debug,通常还会获得结果代码2

切换回release为我做了诀窍。