我正在尝试使用Google Places API获取附近地点的列表。我已经在Google开发者控制台中启用了必需的API并拥有有效的API密钥。我能够获得我的当前位置。在获得我的当前位置后,我尝试获取附近地点列表,但返回值为空。
我还尝试过提供null
PlaceFilter
,但结果相同。
private class MyGoogleMapOnMyLocationListener implements
OnMyLocationChangeListener,
ConnectionCallbacks,
OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
@Override
public void onMyLocationChange(Location location) {
if (!hasLastLocation) {
hasLastLocation = true;
mLastLocation = location;
Log.d(LOG_TAG, "Location: " + location.toString());
if (googleMap != null ) {
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 16.0f));
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient
.Builder(getActivity())
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
}
}
}
@Override
public void onConnected(Bundle bundle) {
Log.d(LOG_TAG, "Google Api Client connected.");
if (mGoogleApiClient != null) {
Log.d(LOG_TAG, "Getting nearby places...");
PendingResult<PlaceLikelihoodBuffer> result =
Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null);
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
@Override
public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
Log.d(LOG_TAG, "Got results: " + likelyPlaces.getCount() + " place found.");
for (PlaceLikelihood placeLikelihood : likelyPlaces) {
Log.i(LOG_TAG, String.format("Place '%s' has likelihood: %g",
placeLikelihood.getPlace().getName(),
placeLikelihood.getLikelihood()));
}
likelyPlaces.release();
}
});
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
这是我的 AndroidManifest.xml
这是我的地图Fragment
我遵循了Google Places API for Android的指示
我尝试了这个问题的被接受的答案: 在Android中使用Google Maps API显示当前位置、附近地点和路线
更新
我已经从Google Developer Console启用了其他与地图相关的API,并且它起作用了。
compile 'com.google.android.gms:play-services-location:8.1.0'
。 - Olcay Ertaş