3

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

  @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获得位置权限后,

在此处输入图像描述

什么都没有发生!在 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')

我也尝试过这个,但从来没有success调用过方法,也没有发生任何事情。

4

2 回答 2

1

This works now(all platforms including web), June 2021, with the Location package.

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

See full details here: pub.dev/packages/location

于 2021-06-26T04:28:45.337 回答
0

Dart 当前的 Geolocation API 存在问题。考虑编写一个互操作库或使用我的。地理位置 PolyFill下面的链接

于 2021-01-27T15:28:55.220 回答