如何要求用户打开位置

Posted

技术标签:

【中文标题】如何要求用户打开位置【英文标题】:How to ask user to turn on location 【发布时间】:2020-08-03 23:22:55 【问题描述】:

如何在下图中提示用户以这种方式打开 GPS。我尝试使用警报对话框方法,但它进入了我希望打开 gps 的设置视图,而不像大多数应用程序那样移动到设置活动。我怎样才能实现它

【问题讨论】:

这能回答你的问题吗? android: Drag map while keeping marker at the center 最简单的解决方案:只需将带有标记图像的ImageView 放在您的地图上。 【参考方案1】:

这适用于 java AndroidX 2020 更新:

private void enableLoc() 



    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(30 * 1000);
    locationRequest.setFastestInterval(5 * 1000);


    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(locationRequest);

    builder.setAlwaysShow(true);

    Task<LocationSettingsResponse> result =
            LocationServices.getSettingsClient(this).checkLocationSettings(builder.build());

    result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() 


        @Override
        public void onComplete(Task<LocationSettingsResponse> task) 
            try 
                LocationSettingsResponse response = task.getResult(ApiException.class);
                // All location settings are satisfied. The client can initialize location
                // requests here.

             catch (ApiException exception) 
                switch (exception.getStatusCode()) 
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        // Location settings are not satisfied. But could be fixed by showing the
                        // user a dialog.
                        try 
                            // Cast to a resolvable exception.
                            ResolvableApiException resolvable = (ResolvableApiException) exception;
                            // Show the dialog by calling startResolutionForResult(),
                            // and check the result in onActivityResult().
                            resolvable.startResolutionForResult(
                                    Maps.this,
                                    LOCATION_SETTINGS_REQUEST);
                         catch (IntentSender.SendIntentException e) 
                            // Ignore the error.
                         catch (ClassCastException e) 
                            // Ignore, should be an impossible error.
                        
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        // Location settings are not satisfied. However, we have no way to fix the
                        // settings so we won't show the dialog.
                        break;
                
            
        
    );


【讨论】:

【参考方案2】:

尝试使用此代码在您的应用中获得类似谷歌地图的权限:

private fun checkPermissions(): Boolean 
        if (ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_COARSE_LOCATION
            ) == PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_FINE_LOCATION
            ) == PackageManager.PERMISSION_GRANTED
        ) 
            return true
        
        return false
    

    private fun requestPermissions() 
        ActivityCompat.requestPermissions(
            this,
            arrayOf(
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION
            ),
            PERMISSION_ID
        )
    

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String>,
        grantResults: IntArray
    ) 
        if (requestCode == PERMISSION_ID) 
            if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) 
                getLastLocation()
            
        
    

【讨论】:

以上是关于如何要求用户打开位置的主要内容,如果未能解决你的问题,请参考以下文章

要求用户打开位置

用户拒绝使用后如何提示用户打开位置服务

打开我的应用程序时如何显示用户的当前位置

如何在不滚动的情况下直接降落在特定位置?

即使用户没有打开应用程序,是不是可以在 android 中跟踪 GPS 位置

如何提示用户打开位置?