Geo Data API不返回任何结果

时间:2017-04-10 09:01:31

标签: android google-places-api

我试图通过将place_id传递给Places.GeoDataApi来获得一个位置,但它没有返回任何内容,我确信place_id是正确的。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Places.GEO_DATA_API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).build();

    String placeId = "ChIJja4QaeH0GjkRKK5cBjTfKQo";

    Places.GeoDataApi.getPlaceById(mGoogleApiClient, placeId)
            .setResultCallback(new ResultCallback<PlaceBuffer>() {
                @Override
                public void onResult(@NonNull PlaceBuffer places) {
                    if(places.getStatus().isSuccess() && places.getCount()>0){
                        final Place myPlace = places.get(0);
                        Log.i(String.valueOf(MainActivity.this),
                                "Place found: " + myPlace.getName() + "\n Address: " + myPlace.getAddress());
                    } else {
                        Log.i(String.valueOf(MainActivity.this),"Place not found!");
                    }
                    places.release();
                }
            });
}

编辑:添加了manifest.xml文件

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<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:roundIcon="@mipmap/ic_launcher_round"
    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>

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

</application>

1 个答案:

答案 0 :(得分:1)

代码与我实现的代码非常相似......除了一件事。

您忘了拨打mGoogleApiClient.connect();

你的代码中的

mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addApi(Places.GEO_DATA_API)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this).build();

mGoogleApiClient.connect(); //add this

//now call the placeID codes Result callback

修改

略微更新条件:

 if(places.getStatus().isSuccess()){
    final Place myPlace = places.get(0);
    Log.i(String.valueOf(MainActivity.this),
            "Place found: " + myPlace.getName() + "\n Address: " + myPlace.getAddress());
} else {
    Log.i(String.valueOf(MainActivity.this),"Place not found!");
}