【问题标题】:getPlaceById() fails to return data on Android google places API / resultCallback not getting calledgetPlaceById() 无法返回 Android google 地方 API / resultCallback 上的数据未被调用
【发布时间】:2015-06-30 08:18:58
【问题描述】:

我无法在 Android 上使用 Google Places API 检索地点详细信息。我正在为特定的 place_id 调用 getPlacyById()。

在一个 Android 活动中,我设置了一个 googleApiClient 并调用 connect()。 apiClient 连接成功。

然后出于测试目的,我使用硬编码的 place_id 和 resultCallback 调用 getPlaceById()。但是,resultCallback 永远不会被调用。

硬编码的 place_id 通过 javascript(使用 javascript Google Maps API)进行测试并返回有效数据。但下面的 Java 代码无法返回任何数据。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.some_activity_layout);
    ButterKnife.inject(this);
    if (getActionBar() != null) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    if (mGoogleApiClient == null) {
        rebuildGoogleApiClient();
    }
    .....
    code_for_activity_init
    .....
}

protected synchronized void rebuildGoogleApiClient() {
    // When we build the GoogleApiClient we specify where connected and connection failed
    // callbacks should be returned, which Google APIs our app uses and which OAuth 2.0
    // scopes our app requests.
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            //.enableAutoManage(this, 0 /* clientId */, this)
            .addConnectionCallbacks(this)
            .addApi(Places.GEO_DATA_API)
            .build();
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    mGoogleApiClient.disconnect();
}


@Override
public void onConnected(Bundle bundle) {
    // Successfully connected to the API client. Pass it to the adapter to enable API access.
    //addressesAdapter.setGoogleApiClient(mGoogleApiClient);
    Log.i("place api conn", "GoogleApiClient connected.");

}

@Override
public void onConnectionSuspended(int i) {
    // Connection to the API client has been suspended. Disable API access in the client.
    //mAdapter.setGoogleApiClient(null);
    Log.e("place api conn", "GoogleApiClient connection suspended.");
}

@OnLongClick(R.id.one_more_request)
public boolean getDataAgain(){
    Log.d("place", "getPlace By Id");
    PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
            .getPlaceById(mGoogleApiClient, "ChIJIV6Mkke9oRQRcPq3KAx513M");
    placeResult.setResultCallback(mUpdatePlaceDetailsCallback);
    return true;
}

private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback
        = new ResultCallback<PlaceBuffer>() {
    @Override
    public void onResult(PlaceBuffer places) {
        if (!places.getStatus().isSuccess()) {
            // Request did not complete successfully
            Log.e("place", "Place query did not complete. Error: " + places.getStatus().toString());

            return;
        }
        // Get the Place object from the buffer.
        final Place place = places.get(0);

        // Format details of the place for display and show it in a TextView.
        //mPlaceDetailsText.
                Toast.makeText(MyActivity.this,/*setText(*/
                        formatPlaceDetails(getResources(), place.getName(),
                place.getId(), place.getAddress(), place.getPhoneNumber(),
                place.getWebsiteUri()), Toast.LENGTH_LONG).show();

        Log.i("place", "Place details received: " + place.getName());
    }
};

这个问题类似于Google Places Api resultCallback not firing 但是我检查了我的 AndroidManifest 已经包含所需的权限

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

最终,我希望每次用户从自动完成地址字段中选择一个项目时调用 getPlaceById()。

***** 更新 ***** 找到解决方案。我错过了两件事。首先,这次将 maps.v2.API_KEY 复制粘贴为 geo.API_KEY(并确保删除 maps.v2.API_KEY,因为它不能指定两次)

android:name="com.google.android.maps.v2.API_KEY" android:value="whatEverGoogleMapsApiKeyYouHave"

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

其次,我必须为 Android 启用 Google Places API

【问题讨论】:

    标签: java android google-maps google-places-api


    【解决方案1】:

    SDK 21 更新

    如果您同时包含两个元数据,您将收到以下错误:

     Caused by: java.lang.RuntimeException: The API key can only be specified once.
    

    而只是包含地理 API 密钥:

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

    而不是地图之一

    【讨论】:

      【解决方案2】:

      这正是我一直在努力解决的问题。在 Android Studio 中,当我添加第二个 API 密钥时,我收到了一个异常:

       Caused by: java.lang.RuntimeException: The API key can only be specified once.
      

      只需将“com.google.android.geo.API_KEY”元数据条目添加到清单文件并删除地图项即可解决此问题。

      【讨论】:

        【解决方案3】:

        我错过了两件事。

        首先是与 com.google.android.maps.v2.API_KEY 相同的 com.google.android.geo.API_KEY。只需将 API KEY 复制粘贴到 AndroidManifest 文件中即可。

        android:name="com.google.android.maps.v2.API_KEY" android:value="whatEverGoogleMapsApiKeyYouHave"

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

        我也错过了谷歌控制台上的 Google Places API for Android 服务。如果您启用了 Google Places API,请注意,这还不够,因为它只是一个不同的 API。您必须转到 google 控制台并明确启用适用于 Android 的 Google Places API。

        ****** 编辑 ******** 正如@Lissy 指出的那样,从 SDK 21 开始,定义相同的 API_KEY 两次将产生错误。保留 geo.API_KEY 就足够了

        【讨论】:

        • 你好 kouretinho;感谢您更新解决方案!我建议您接受自己的答案,以便其他人都能从您的工作中受益。
        • 嗨@plexer,是的,我会接受答案,我只需要再等一天!
        • @kouretinho 这个答案不再正确,它将返回 SDK 21 及更高版本的错误。查看更新
        猜你喜欢
        • 1970-01-01
        • 2016-02-06
        • 2015-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-04
        • 2018-02-26
        • 2018-05-27
        相关资源
        最近更新 更多