在地图上罚款位置权限被拒绝转到自定义位置 [英] On Map Fine Location Permission Denied Go To Custom Location

查看:115
本文介绍了在地图上罚款位置权限被拒绝转到自定义位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作使用地图API的应用程序。我想要的是做一个权限弹出。当用户点击允许时,它应该放大到他当前的位置。现在我想决定如果他不想允许这样做会发生什么。如果他拒绝这个权限地图应放大到自定义位置,但永远不会去那里,它不显示吐司。



我的地图片段看起来像这样



请求许可此处:



$ p $ mapView.getMapAsync(new OnMapReadyCallback(){
@Override
public void onMapReady(GoogleMap googleMap){
mMap = googleMap;

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

} else {
ActivityCompat.requestPermissions(getActiv ity(),new String [] {Manifest.permission.ACCESS_FINE_LOCATION},MY_LOCATION_REQUEST_CODE);
}
return;
}

mMap.setMyLocationEnabled(true);
LocationManager locationManager =(LocationManager)getActivity()。getSystemService(Context.LOCATION_SERVICE);
标准标准=新标准();

//禁用地图工具栏:
mMap.getUiSettings()。setMapToolbarEnabled(false);

//获取最佳提供者和当前位置的名称
String provider = null;
if(locationManager!= null){
provider = locationManager.getBestProvider(criteria,true);
}
//获取当前位置
位置myLocation = null;
if(locationManager!= null){
myLocation = locationManager.getLastKnownLocation(provider);
}

//设置默认的纬度和经度为格林威治
双纬度= 51.4825766;
双经度= -0.0076589;

//获取当前位置和LatLng对象的经度和纬度
if(myLocation!= null){
latitude = myLocation.getLatitude();
longitude = myLocation.getLongitude();
}

CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude,longitude))。zoom(14).build();
mMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));

$ b $ / code>

em>

  @Override $ b $ public void onRequestPermissionsResult(int requestCode,String [] permissions,int [ ] grantResults){
if(requestCode == MY_LOCATION_REQUEST_CODE){
if(permissions.length == 1&& $ amp;
permissions [0] == Manifest.permission.ACCESS_FINE_LOCATION&& amp ;
grantResults [0] == PackageManager.PERMISSION_GRANTED){
if(ActivityCompat.checkSelfPermission(getContext(),Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED&&& ActivityCompat.checkSelfPermission(getContext (),Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
// TODO:考虑调用
// ActivityCompat#requestPermissions
//在这里请求缺少的权限,然后overrid
// public void onRequestPermissionsResult(int requestCode,String [] permissions,
// int [] grantResults)
//处理用户授予权限的情况。有关更多详细信息,请参阅文档
//获取ActivityCompat#requestPermissions。
return;
}
mMap.setMyLocationEnabled(true);
} else {
//权限被拒绝。显示一条错误消息。
双纬度= 51.4825766;
双经度= -0.0076589;
Toast.makeText(getContext(),Permission denied,Toast.LENGTH_SHORT).show(); CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude,longitude))。zoom(14).build();
mMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));




解决方案

我将权限放在父活动中,以便它能够正常工作。


I am making an app that uses maps API. What I want is to make a permission popup. When the user clicks allow it should zoom in to his current location. Now I want to decide what happens if he doesn't want to allow this. If he denies this permission map should zoom to the custom location, but in never goes there and it doesn't show the toast.

My maps fragment looks like this:

REQUESTING PERMISSION HERE:

 mapView.getMapAsync(new OnMapReadyCallback() {
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

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

            } else {
                ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_LOCATION_REQUEST_CODE);
            }
            return;
        }

        mMap.setMyLocationEnabled(true);
        LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();

        //Disable Map Toolbar:
        mMap.getUiSettings().setMapToolbarEnabled(false);

        // Get the name of the best provider and current location
        String provider = null;
        if (locationManager != null) {
            provider = locationManager.getBestProvider(criteria, true);
        }
        // Get current location
        Location myLocation = null;
        if (locationManager != null) {
            myLocation = locationManager.getLastKnownLocation(provider);
        }

        // Set default latitude and longitude to Greenwich
        double latitude = 51.4825766;
        double longitude = -0.0076589;

        // Get latitude and longitude of the current location and a LatLng object
        if (myLocation != null) {
            latitude = myLocation.getLatitude();
            longitude = myLocation.getLongitude();
        }

        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(new LatLng(latitude, longitude)).zoom(14).build();
        mMap.animateCamera(CameraUpdateFactory
                .newCameraPosition(cameraPosition));

}

CATCHING RESULTS HERE:

    @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == MY_LOCATION_REQUEST_CODE) {
        if (permissions.length == 1 &&
                permissions[0] == Manifest.permission.ACCESS_FINE_LOCATION &&
                grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_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;
            }
            mMap.setMyLocationEnabled(true);
        } else {
            // Permission was denied. Display an error message.
            double latitude = 51.4825766;
            double longitude = -0.0076589;
            Toast.makeText(getContext(), "Permission denied", Toast.LENGTH_SHORT).show();          CameraPosition cameraPosition = new CameraPosition.Builder()
                            .target(new LatLng(latitude, longitude)).zoom(14).build();
                    mMap.animateCamera(CameraUpdateFactory
                            .newCameraPosition(cameraPosition));
        }
    }
}

解决方案

I put the permission in the parent activity in order for it to work.

这篇关于在地图上罚款位置权限被拒绝转到自定义位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆