getPlaceById()无法在谷歌Android的地方API返回的数据/ resultCallback没有得到所谓的 [英] getPlaceById() fails to return data on Android google places API / resultCallback not getting called

查看:529
本文介绍了getPlaceById()无法在谷歌Android的地方API返回的数据/ resultCallback没有得到所谓的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能检索使用Android上的谷歌Places API的位置详情。我打电话getPlacyById()具体place_id。

I cannot retrieve Place Details using the Google Places API on Android. I'm calling getPlacyById() for specific place_id.

在一个Android的活动我设置一个googleApiClient和调用connect()。该apiClient成功连接。

Inside an Android activity I setup a googleApiClient and call connect(). The apiClient connects successfully.

然后用于测试目的我呼吁getPlaceById()使用硬codeD place_id和resultCallback。然而,resultCallback不会被调用。

Then for testing purposes I call getPlaceById() using a hardcoded place_id and a resultCallback. However, the resultCallback never gets called.

硬codeD place_id是通过JavaScript测试(使用JavaScript谷歌地图API),并返回有效的数据。但低于的Java code未能返回任何数据。

The hardcoded place_id is tested through javascript (using the javascript Google Maps API) and returns valid data. but the Java code below fails to return any data.

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

这个问题是类似<一个href=\"http://stackoverflow.com/questions/29744045/google-places-api-android-resultcallback-not-firing\">Google Places API的resultCallback不费一枪但是我已经检查了我的Andr​​oidManifest已经包含了所需的权限

this question is similar to Google Places Api resultCallback not firing however I have checked that my AndroidManifest already includes the the required permission

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

最后我想getPlaceById()被调用每一个用户从一个自动完成地址字段选择一个项目的时间。

Ultimately I would like the getPlaceById() to be called every time the user selects an item from an autocomplete Address field.

*****更新*******
解决方案中。我错过了两件事情。首先,复制粘贴此时间geo.API_KEY的maps.v2.API_KEY(并确保您删除maps.v2.API_KEY,因为它不能被指定两次)

***** UPDATE ******* Solution found. I was missing two things. First, copy-paste the maps.v2.API_KEY this time as geo.API_KEY (and make sure you delete the maps.v2.API_KEY, because it cannot be specified twice)

<罢工>的android:NAME =com.google.android.maps.v2.API_KEY机器人:值=whatEverGoogleMapsApiKeyYouHave

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

其次,我不得不启用谷歌Android Places API的

and secondly I had to enable Google Places API for Android

推荐答案

我错过了两件事情。

首先这是一样的com.google.android.maps.v2.API_KEY。只要复制粘贴就AndroidManifest文件中的API密钥。

First of all the com.google.android.geo.API_KEY which is the same as the com.google.android.maps.v2.API_KEY. Just copy paste the API KEY on the AndroidManifest file.

<罢工>的android:NAME =com.google.android.maps.v2.API_KEY机器人:值=whatEverGoogleMapsApiKeyYouHave

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

我还缺少针对Android在谷歌Places API的服务谷歌控制台上。如果你的谷歌Places API的启用,要小心,这还不够,因为它只是一个不同的API。你必须去谷歌控制台并明确启用Android的谷歌的Places API的。

I was also missing the Google Places API for Android Service on google console. If you have Google Places API enabled , beware, this in not enough, because it's simply a different API. You have to go to google console and explicitly enable the Google Places API for Android.

******编辑********
由于@Lissy指出,从SDK 21日起,定义两次同样的API_KEY会给出错误。保持geo.API_KEY就足够了。

****** EDIT ******** As @Lissy pointed out, from SDK 21 onwards, defining the same API_KEY twice will give an error. Keeping the geo.API_KEY will suffice

这篇关于getPlaceById()无法在谷歌Android的地方API返回的数据/ resultCallback没有得到所谓的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆