【问题标题】:flutter-web: get location on web by Location pluginflutter-web:通过 Location 插件获取 web 上的位置
【发布时间】:2021-02-01 06:44:45
【问题描述】:

我有旧的颤振项目,我添加了网络支持,现在,我正在尝试在颤振网络中获取我的位置,所以我在我的页面上添加了 location。 这是我的代码:

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

  Future _getLocation() async {
    Location location = new Location();

    var _permissionGranted = await location.hasPermission();
    _serviceEnabled = await location.serviceEnabled();

    if (_permissionGranted != PermissionStatus.granted || !_serviceEnabled) {
      _permissionGranted = await location.requestPermission();
      _serviceEnabled = await location.requestService();
    } else {
      print("-----> $_serviceEnabled");

      setState(() {
        _serviceEnabled = true;
        _loading = false;
      });
    }

    try {
      final LocationData currentPosition = await location.getLocation();
      setState(() {
        longitude = currentPosition.longitude.toString();
        latitude = currentPosition.latitude.toString();
        print(
            '${widget.url}?BranchName=&latitude=${latitude}&longitude=${longitude}');
        _loading = false;
      });
    } on PlatformException catch (err) {
      _loading = false;
      print("-----> ${err.code}");
    }
  }

通过chrome获得Location权限后,

什么都没发生! 在 vsCode 控制台中,我刚刚收到此错误:

Error: [object GeolocationPositionError]


    at Object.createErrorWithStack (http://localhost:43705/dart_sdk.js:4351:12)
    at Object._rethrow (http://localhost:43705/dart_sdk.js:37962:16)
    at async._AsyncCallbackEntry.new.callback (http://localhost:43705/dart_sdk.js:37956:13)
    at Object._microtaskLoop (http://localhost:43705/dart_sdk.js:37788:13)
    at _startMicrotaskLoop (http://localhost:43705/dart_sdk.js:37794:13)
    at http://localhost:43705/dart_sdk.js:33303:9

**使用@JS('navigator.geolocation')

我也 try this,但从未调用过 success 方法,但什么也没发生。

【问题讨论】:

  • 那你解决了吗?

标签: flutter location flutter-web getlocation


【解决方案1】:

Dart 目前的 Geolocation API 存在问题。考虑编写一个互操作库或使用我的。以下链接Geolocation PolyFill

【讨论】:

    【解决方案2】:

    现在(包括网络在内的所有平台),2021 年 6 月,与 Location 包一起工作。

    final Location location = new Location();
    _locationData = await location.getLocation();
    print(_locationData.latitude);
    

    在此处查看完整的详细信息: pub.dev/packages/location

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-29
      • 1970-01-01
      • 2021-12-15
      • 2020-09-22
      • 2021-10-03
      • 2021-05-22
      • 2020-05-03
      • 2011-01-31
      相关资源
      最近更新 更多