在Flutter上进行位置设置的本机对话框

时间:2019-11-22 13:06:51

标签: flutter

有没有一种方法可以实现“位置”对话框的设置,例如下图所示,当应用需要GPS定位但找不到GPS时会触发该对话框。点击确定将立即打开系统GPS。对于用户而言,这似乎比将他们带到位置并手动打开更方便。 可以在Flutter中实现这种事情吗?

enter image description here

对话框的展开视图:

enter image description here

2 个答案:

答案 0 :(得分:0)

根据here,感谢Rajesh。 plugin使您可以添加此本机对话框以进行快速位置设置。

实现非常简单:

import 'package:location/location.dart';

var location = Location();

Future _checkGps() async {
if(!await location.serviceEnabled()){
   location.requestService();
  }
}

@override
void initState() {
  super.initState();
  _checkGps();
}

答案 1 :(得分:0)

在Kotlin中,尝试以下代码:

class HomeActivity : AppCompatActivity() {
    private val requestLocation = 199

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableLoc()
    }
    private fun enableLoc() {
        val mLocationRequest = LocationRequest.create()
        mLocationRequest.interval = 10000
        mLocationRequest.fastestInterval = 5000
      //  mLocationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY

        val builder = LocationSettingsRequest.Builder()
            .addLocationRequest(mLocationRequest)

        val client = LocationServices.getSettingsClient(this)
        val task =
            client.checkLocationSettings(builder.build())

        task.addOnSuccessListener(this) {
            // All location settings are satisfied. The client can initialize
            // location requests here.
            // ...
        }

        task.addOnFailureListener(this) { e ->
            if (e is ResolvableApiException) {
                // Location settings are not satisfied, but this can be fixed
                // by showing the user a dialog.
                try {
                    // Show the dialog by calling startResolutionForResult(),
                    // and check the result in onActivityResult().
                    e.startResolutionForResult(
                        this,
                        requestLocation
                    )
                } catch (sendEx: SendIntentException) {
                    // Ignore the error.
                }
            }
        }
    }
}