无法使用Google的LocationServices API获取用户位置

时间:2018-02-20 16:16:41

标签: android google-maps google-api

我正在尝试从用户那里获取位置,但是当我为该位置输入以下请求代码时:

private void getLocation() {
    mLastLocation = LocationServices.FusedLocationApi
            .getLastLocation(mGoogleApiClient);

    if (mLastLocation != null) {
        double latitude = mLastLocation.getLatitude();
        double longitude = mLastLocation.getLongitude();
    }
}

崩溃是因为它获得了mLastLocation null

我在onResume()方法中调用该函数但没有成功。但是如果我把它放在一个按钮点击回调中,它就会得到位置。

有没有办法在应用程序加载后获取位置?

1 个答案:

答案 0 :(得分:1)

首先,您应该检查您是否具有发出此请求所需的权限。

连接到Google的LocationServices API后,您应该获得用户位置。

然后你应该使用FusedLocationProviderClient.getLastLocation()返回的Task异步请求位置

以下是一些可以帮助您的代码:

GoogleApiClient mGoogleApiClient;

private void getGoogleApiClient() {
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                .addApi(LocationServices.API)
                .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                    @Override
                    public void onConnected(@Nullable Bundle bundle) {
                        FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getActivity());

                        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                            Log.e(getClass().getName(), "Location permission not granted");
                            return;
                        }

                        Task task = mFusedLocationClient.getLastLocation();

                        task.addOnSuccessListener(getActivity(), new OnSuccessListener<Location>() {
                            @Override
                            public void onSuccess(Location location) {
                                // Got last known location. In some rare situations this can be null.
                                if (location != null) {
                                    double latitude = location.getLatitude();
                                    double longitude = location.getLongitude();
                                }
                            }
                        });
                    }

                    @Override
                    public void onConnectionSuspended(int i) {
                        Log.e(getClass().getName(), "onConnectionSuspended() ");
                    }
                })
                .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                        Log.e(getClass().getName(), "Get location failure : " + connectionResult.getErrorMessage());
                    }
                })
                .build();
    }
    mGoogleApiClient.connect();
}

只需在需要位置时调用方法getGoogleApiClient()即可。 确保您的设备上已激活GPS位置。

有关此主题的更多信息:

https://developer.android.com/training/location/retrieve-current.html