2017-02-13 97 views
0

我一直在尝试测试Google Places API的getCurrentPlace()方法。当我第一次测试它时,它没有问题,但现在突然停止工作。我卸载了应用程序,删除了代码并从文档中重新粘贴,关闭了Android Studio。我不知道发生了什么问题,并且日志没有发生错误,他们只是不记录过去记录的内容。getCurrentPlace onResult不返回结果

View.OnClickListener getNearbyPlaces = new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     places = new FindNearbyPlaces(); 
     placesArray = new ArrayList<>(); 
     try { 
      placesArray = places.findNearbyPlaces(mCurrentLocation); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     for (PlacesAttributes place : placesArray) { 
      Log.i("Name: ", place.getName()); 
      Log.i("Lat: ", String.valueOf(place.getLat())); 
      Log.i("Lng: ", String.valueOf(place.getLng())); 
     } 

     if (ActivityCompat.checkSelfPermission(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. 
      return; 
     } 
     PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi 
       .getCurrentPlace(mGoogleApiClient, null); 
     result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() { 
      @Override 
      public void onResult(PlaceLikelihoodBuffer likelyPlaces) { 
       Log.i("help", ""); 
       for (PlaceLikelihood placeLikelihood : likelyPlaces) { 
        Log.i("Place", String.format("Place '%s' has likelihood: %g", 
          placeLikelihood.getPlace().getName(), 
          placeLikelihood.getLikelihood())); 
       } 
       likelyPlaces.release(); 
      } 
     }); 


    } 
}; 

这是我有的代码块。在开始的前三个日志语句运行他们的过程,但onResult中的日志语句不,甚至没有Log.i(“帮助”,“”)。我有开始日志声明他们在什么时候工作,所以他们不应该是一个问题。

回答

0

如果应用程序没有ACCESS_FINE_LOCATION权限,您提供的代码示例会提前返回。也许这就是发生了什么事。检查授予的权限(设置 - >应用程序 - > AppName->权限)。

+0

我没有检查,只是为了确保。但是你看不到,这是我的道歉,getCurrentPlace()之前的上述语句制定了我的另一个功能,它通过URL执行PlaceSearch,这样我就可以通过日志访问我的位置并返回附近的地方。 –

0

经过大量的工作(启用Places Android API,添加更多的按键,删除按键,重命名按键,在元数据下的“name”字段中重命名“API_KEY”),该应用终于看到了所有结果突然间。我真的不知道我采取了哪些行动解决了它,但现在看来它正在工作。

0

很多人错过的是错误很可能与设备的位置服务被关闭的事实相关联。错误过于通用,不能让开发人员知道发生了什么事情,并且如果没有时间试图弄清楚发生了什么,开发人员通常会花费数小时的时间。

检查设备设置下的位置服务

0

您应该检查连接。

GoogleApiClient mGoogleApiClient = new GoogleApiClient 
      .Builder(getContext()) 
      .addApi(Places.GEO_DATA_API) 
      .addApi(Places.PLACE_DETECTION_API) 
      .build(); 

    mGoogleApiClient.connect();