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

相關問題