I used the Place API for Android to try to find nearby places using Places.PlaceDetectionApi.getCurrentPlace ().
However, when I launch the application and try to find neighboring places, I always get no results.
I am sure that I set the permission "ACCESS_FINE_LOCATION" and enabled the "Google Place API", and also set the corresponding API_KEY.
The following is the action I used.
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();
}
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();
}
}
source
share