天天看點

Flutter使用高德地圖擷取定位Flutter使用高德地圖擷取定位

Flutter使用高德地圖擷取定位

Github位址:高德定位Flutter插件

具體配置項可查閱官方說明文檔

1. 初始化變量

Map<String, Object> _locationResult;
StreamSubscription<Map<String, Object>> _locationListener;
AmapLocationFlutterPlugin _locationPlugin = new AmapLocationFlutterPlugin();
           

2. 設定API Key并調用定位方法

void initState() {
    super.initState();
    AmapLocationFlutterPlugin.setApiKey(
        'Android Key', 'iOS Key');
    _registerListener();
}
           

3. 定位方法

_registerListener() async {
    // 請求系統定位權限
    await _handlePermission();
    // 配置定位的參數
    _setLocationOption();
    // 擷取定位結果的監聽方法
    _locationListener = _locationPlugin
        .onLocationChanged()
        .listen((Map<String, Object> result) {
      setState(() {
        print(result);
        // 将結果存在變量中
        _locationResult = result;
    });
    // 開始定位
    // 如果不需要在initState階段調用定位則不需要這句
    _locationPlugin.startLocation();
}
           

4. 擷取權限的方法

Future<void> _handlePermission() async {
  // 使用permission_handler: ^5.0.1+1元件擷取權限
  await Permission.location.request();
}
           

5. 設定定位參數的方法

void _setLocationOption() {
    if (null != _locationPlugin) {
      AMapLocationOption locationOption = new AMapLocationOption();

      ///是否單次定位
      locationOption.onceLocation = true;

      ///是否需要傳回逆地理資訊
      locationOption.needAddress = true;

      ///逆地理資訊的語言類型
      locationOption.geoLanguage = GeoLanguage.ZH;

      locationOption.desiredLocationAccuracyAuthorizationMode =
          AMapLocationAccuracyAuthorizationMode.ReduceAccuracy;

      locationOption.fullAccuracyPurposeKey = "AMapLocationScene";

      ///設定Android端連續定位的定位間隔
      locationOption.locationInterval = 2000;

      ///設定Android端的定位模式<br>
      ///可選值:<br>
      ///<li>[AMapLocationMode.Battery_Saving]</li>
      ///<li>[AMapLocationMode.Device_Sensors]</li>
      ///<li>[AMapLocationMode.Hight_Accuracy]</li>
      locationOption.locationMode = AMapLocationMode.Hight_Accuracy;

      ///設定iOS端的定位最小更新距離<br>
      locationOption.distanceFilter = -1;

      ///設定iOS端期望的定位精度
      /// 可選值:<br>
      /// <li>[DesiredAccuracy.Best] 最高精度</li>
      /// <li>[DesiredAccuracy.BestForNavigation] 适用于導航場景的高精度 </li>
      /// <li>[DesiredAccuracy.NearestTenMeters] 10米 </li>
      /// <li>[DesiredAccuracy.Kilometer] 1000米</li>
      /// <li>[DesiredAccuracy.ThreeKilometers] 3000米</li>
      locationOption.desiredAccuracy = DesiredAccuracy.Best;

      ///設定iOS端是否允許系統暫停定位
      locationOption.pausesLocationUpdatesAutomatically = false;

      ///将定位參數設定給定位插件
      _locationPlugin.setLocationOption(locationOption);
    }
}
           

6. 通過按鈕觸發定位

RaisedButton(
  child: Text('點選定位'),
  onPressed: () {
    _locationPlugin.startLocation();
  }
)
           

7. 銷毀定位

@override
  void dispose() {
    super.dispose();
    // 停止定位
    if (null != _locationPlugin) {
      _locationPlugin.stopLocation();
    }

    // 移除定位監聽
    if (null != _locationListener) {
      _locationListener.cancel();
    }

    // 銷毀定位
    if (null != _locationPlugin) {
      _locationPlugin.destroy();
    }
  }