0

我开始与谷歌的地方apis,我想获得设备的当前位置,这是我迄今为止所做的,但我无法弄清楚如何以及何时OnResult内的代码被称为,因为它里面的日志不会被印刷在logcat中:什么时候在应用程序中调用OnResult? (ResultCallback)

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

您的代码位于try-catch块内。在catch中放入一些日志或断点,看看你的代码是否抛出一些异常。 – androidevil

+0

尝试通过添加登录catch块,没有发现异常 – user3820753

回答

1

您需要注册一个API密钥,否则结果将不会显示。这里

注册:https://developers.google.com/places/android-api/signup

,然后在AndroidManifest.xml,包括:

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

编辑: 所以我说你的代码到一个新的项目,看到onResult()从未执行。然而,经过一些改变,我能够得到它的工作。

首先,我做了我的活动FragmentActivity

然后我用的是GoogleApiClient.Builder()像这样:

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

这样做后,我看到onResult()现在正执行。

+0

非常感谢您的时间,我发现问题是我从来没有添加代码来连接GoogleApiClient,即.enableAutoManage(this,this) – user3820753

0

你需要在你的AndroidManifest.xmlACCESS_FINE_LOCATION权限集。如果没有设置位置数据将不可用,结果将永远不会发布。

+0

但我已经在清单中添加了此权限,元数据中的geo.api键和readgservices权限也存在于清单中。 – user3820753

相关问题