不推荐使用GoogleApiClient.OnConnectionFailedListener吗?

时间:2019-12-12 11:28:00

标签: android google-maps google-api-client

我使用GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener(不建议使用)获得当前的纬度和经度。我们用来获取位置而不是GoogleApiClient的位置是什么?

此处的Java代码:

   private fun createGoogleApiClient(): GoogleApiClient? {
    return GoogleApiClient.Builder(mContext).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build()
}
override fun onConnected(p0: Bundle?) {

    if (!checkPermissions()) {

        requestPermissions()
    } else {
        val location = LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest) { location: Location? -> onLocationChanged(location) }



        if (location == null) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest) { location: Location? ->
                onLocationChanged(location)
            }
        } else {


        }
    }


}

   override fun onLocationChanged(p0: Location?) {

    if (p0 != null) {
        mCurrentLocation = p0
    }

}

1 个答案:

答案 0 :(得分:0)

不建议使用GoogleAPIClient。为了获得用户的位置,您必须使用融合的位置提供程序。通过下面的链接,您将了解如何使用它:-

https://www.vogella.com/tutorials/AndroidLocationAPI/article.html