如何在Android Lollipop中使用融合位置提供程序客户端

时间:2019-03-28 06:47:20

标签: android geolocation location android-gps fusedlocationproviderclient

我正在尝试使用“ FusedLocationProviderClient”获取当前位置以在地图上显示它 但它在android棒棒糖中不起作用,尽管在上述所有版本中都可以正常工作。下面是代码示例,我正在尝试做。

        if (checkSelfPermission(getActivity(),Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(getActivity(),Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            return v;
        }
        fusedLocationProviderClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                mLastLocation=location;

                getAltitude();
                if(mMap!=null){

                    if(location!=null){
                        Marker marker=mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(),location.getLongitude())));
                        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),location.getLongitude()),15.0f));
                    }
                    }
            }
        });

0 个答案:

没有答案