Search code examples
androidgoogle-apigoogle-play-servicesgoogle-places-apiandroid-pendingintent

When is OnResult called in an application ? (ResultCallback)


I am starting off with google places apis , I am trying to get current location of the device ,this is what i have done so far , but i can't figure out how and when the code inside OnResult is called , because logs inside it are not printed in logcat :

Log.d("2^^check1111","nside : beforee");

    try {
        if (ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            Log.d("2here now","yoyo0");
            ActivityCompat.requestPermissions(getActivity(), new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, 123);
        }

        Log.d("999here now","yoyo0");

        PendingResult<PlaceLikelihoodBuffer> result =  Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null);

        Log.d("888here now","after result");

        result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {

            @Override
            public void onResult(@NonNull PlaceLikelihoodBuffer likelyPlaces) {
             //    utils.showLog(TAG, "" + likelyPlaces.getCount());

               /// Toast.makeText(getActivity().getApplicationContext(),"inside : onResult",Toast.LENGTH_LONG).show();
                Log.d("2^^check","nside : onResul");
                if (likelyPlaces.getCount() > 0) {


                //    Toast.makeText(getActivity().getApplicationContext(),"inside :likely >0",Toast.LENGTH_LONG).show();
                    Log.d("2^^check2","nside :likely >0");
                    PlaceLikelihood placeLikelihood = likelyPlaces.get(0);
                    //                String content = "";
                    if (placeLikelihood != null && placeLikelihood.getPlace() != null && !TextUtils.isEmpty(placeLikelihood.getPlace().getName())) {

                        Log.d("2^^check3","nside : placelikelihood not nu");
                    //    Toast.makeText(getActivity().getApplicationContext(),"inside : placelikelihood not null",Toast.LENGTH_LONG).show();
   LatLng FromLatLng = placeLikelihood.getPlace().getLatLng();
                        Log.d("2^^check4","4444"+FromLatLng);
                     //   Toast.makeText(getActivity().getApplicationContext(),"inside : latlng :"+FromLatLng,Toast.LENGTH_LONG).show();
                        newLatLng = FromLatLng;
                        FromLat = FromLatLng.latitude;
                        FromLng = FromLatLng.longitude;
                    }
                } else {

       Toast.makeText(getActivity(), "Auto Location can't fetch", Toast.LENGTH_SHORT).show();
                }
                likelyPlaces.release();
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Solution

  • You need to register for an API key otherwise results will not be shown.

    Register here: https://developers.google.com/places/android-api/signup

    And then in your AndroidManifest.xml, include:

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

    Edit: So I added your code to a new project and saw that onResult() never executed. However after a few changes I was able to get it working.

    First, I made my activity a FragmentActivity

    Then I use the GoogleApiClient.Builder() like so:

    final GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addApi(Places.PLACE_DETECTION_API)
        .addApi(Places.GEO_DATA_API)
        .enableAutoManage(this, this)
        .build();
    

    After doing this, I see onResult() is now getting executed.