我使用Place API for Android尝试使用Places.PlaceDetectionApi.getCurrentPlace()来附近的地方。
然而,当我运行应用程序并尝试到附近的地方时,我总是没有结果。
我确信我已设置了“ACCESS_FINE_LOCATION”权限,并启用了“Google Place API”,并设置了相应的API_KEY。
以下是我使用的活动。
public class PlaceLikelihoodActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place_likelihood);
mGoogleApiClient = new GoogleApiClient
.Builder(this)
.addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
//function triggered by a button to get nearby places
public void onPlaceLikelihood(View view) {
Log.i("Place", "onPlaceLikelihood");
if (mGoogleApiClient != null) {
PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
.getCurrentPlace(mGoogleApiClient, null);
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
@Override
public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
Log.i("Place", "onResult");
if (likelyPlaces.getCount() <= 0) {
Toast.makeText(PlaceLikelihoodActivity.this, "No place found", Toast.LENGTH_SHORT).show();
}
for (PlaceLikelihood placeLikelihood : likelyPlaces) {
Log.i("Place", String.format("Place '%s' has likelihood: %g",
placeLikelihood.getPlace().getName(),
placeLikelihood.getLikelihood()));
}
likelyPlaces.release();
}
});
}
else{
Toast.makeText(PlaceLikelihoodActivity.this, "No GoogleApiClient", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnected(Bundle bundle) {
Toast.makeText(this, "onConnected", Toast.LENGTH_LONG).show();
}
@Override
public void onConnectionSuspended(int i) {
Toast.makeText(this, "onConnectionSuspended", Toast.LENGTH_LONG).show();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Toast.makeText(this, "onConnectionFailed", Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:0)
Try calling mGoogleApiClient.connect(); in the onCreate method. and maybe check mGoogleApiClient.isConnected() in addition to mGoogleApiClient != null in onPlaceLikelihood.
答案 1 :(得分:0)
您是否启用了API&#34; Google Places API for Android&#34;来自Google Developer Console? 侨
答案 2 :(得分:-1)
我想我知道答案,在模拟器或手机上,保护隐私和安全,并打开位置高精度模式。