Android Place API: PlaceDetectionApi.getCurrentPlace () does not always return results

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

//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();
}
}
+4
source share
3 answers

mGoogleApiClient.connect(); onCreate. , , mGoogleApiClient.isConnected() mGoogleApiClient!= null onPlaceLikelihood.

0

API- Google Places API Android Google? Ciao

0

I think I know the answer, on the emulator or on your phone, go to privacy and security and turn on the high-precision location mode.

-one
source

All Articles