【问题标题】:OSMdroid GPS enable/disable user popupOSMdroid GPS 启用/禁用用户弹出窗口
【发布时间】:2018-02-15 15:45:00
【问题描述】:

我想确保如果用户在应用程序启动时关闭了他们的 GPS 位置,则会出现一个弹出窗口,并将您重定向到可以打开 GPS 位置的位置。 我能够做到这一点,但是一旦我回到我的应用程序,它就不再以我的位置为中心,除非我关闭应用程序并重新启动它。 我做错了什么?

private boolean hasPermissions(){
    return ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
            ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
            ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}

在 onCreate 中:

if (hasPermissions()) {
        setMap();
        initMyLocation();
        centerButton();
    } else {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
    }

setMap、initMyLocation、centerButton 方法:

public void setMap(){
    map = findViewById(R.id.mapview);
    map.setTileSource(TileSourceFactory.MAPNIK);
    map.setTilesScaledToDpi(true);
    map.setBuiltInZoomControls(true);
    map.setMultiTouchControls(true);

}
 public void initMyLocation(){

    final LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);

    if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER) || !lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
        //Build the alert dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Something.");
        builder.setMessage("activate GPS.");
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        });
        Dialog alertDialog = builder.create();
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.show();


    }

    GpsMyLocationProvider provider = new GpsMyLocationProvider(getApplicationContext());

    provider.addLocationSource(LocationManager.GPS_PROVIDER);
    provider.addLocationSource(LocationManager.NETWORK_PROVIDER);
    locationNewOverlay = new MyLocationNewOverlay(provider, map);
    locationNewOverlay.enableMyLocation();

    map.getOverlays().add(locationNewOverlay);


}
public void centerButton() {
    ImageButton btCenterMap = this.findViewById(R.id.menu_mylocation);
    btCenterMap.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            locationNewOverlay.runOnFirstFix(new Runnable() {
                @Override
                public void run() {
                    myLocation = locationNewOverlay.getMyLocation();
                }
            });
            locationNewOverlay.enableFollowLocation();

        }
    });
}

【问题讨论】:

    标签: android osmdroid android-gps


    【解决方案1】:

    我认为你的逻辑需要是这样的:

    检查方法 - 检查权限以及 GPS/网络位置是否可用

    • 如果权限检查失败,请求权限。成功后,再次运行检查方法
    • 如果 gps 检查失败,重定向到设置页面
    • 如果两者都是,则创建位置叠加层并将其添加到地图中

    关于简历

    • 运行检查方法

    暂停

    • 销毁我的位置叠加层,将其从地图中移除

    【讨论】:

    • 确实是我的逻辑有问题。遵循您的提示后,我开始工作了。非常感谢!!
    猜你喜欢
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多