Flutter-无法将标记设置为我当前的位置

时间:2018-07-30 09:15:38

标签: asynchronous async-await flutter future dart-async

我想在调用mapView.show之前在我的currentLocation上放置一个标记 因此,我将序列分为两种方法,一种方法是从位置插件获取当前位置_getCurrentPlace(),另一种方法是显示地图并设置标记showMap() 问题似乎是地图开始显示的速度比从未来方法_getCurrentPlace()中获取currentLocation的速度快,因此它不会放置标记。这里有任何想法或帮助吗?

我使用的插件: map_view:^ 0.0.14 位置:^ 1.3.4

我的构建方法包括一个引发按钮,该按钮调用showMap方法

 @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text("Flutter Google maps"),
      ),
      body: new Container(
          padding: new EdgeInsets.only(top: 25.0),
          child: new RaisedButton(
              key: null,
              onPressed: showMap(),
              color: Colors.red,
              child: new Text(
                "Pick a place",
                style: new TextStyle(
                    fontSize: 15.0,
                    color: Colors.white,
                    fontWeight: FontWeight.bold,
                    fontStyle: FontStyle.normal,
                    fontFamily: "Roboto"),
              ))),
    );
  }

要显示地图并使用 map_view 插件设置标记,我还等待当前位置完成设置为initialcameraPosiotion中的MapOptions

  showMap() async {
    var currentLocation = await _getCurrentPlace();
    mapView.show(
        MapOptions(
          showUserLocation: true,
          title: "Choose a place",
          initialCameraPosition: CameraPosition(
              locMapView.Location(
                  currentLocation["latitude"], currentLocation['longitude']),
              15.0),
        ),
        toolbarActions: <ToolbarAction>[
          ToolbarAction("Close", 1),
        ]);

    mapView.onToolbarAction.listen((id) {
      if (id == 1) {
        print('1');
        mapView.dismiss();
      }
    });
    mapView.onMapReady.listen((_) {
      print("Map ready");
      new Marker("1", "Pick up place! ", currentLocation["latitude"],
          currentLocation['longitude'],
          draggable: true);
    });
  }

这是将来使用 location 插件

获取位置的方法
Future<Map<String, double>> _getCurrentPlace() async {
    var getPresentLocation = <String, double>{};
    try {
      getPresentLocation = await userLocation.getLocation;
      error = null;
    } on PlatformException catch (e) {
      if (e.code == 'PERMISSION_DENIED') {
        error = 'Permission denied';
      } else if (e.code == 'PERMISSION_DENIED_NEVER_ASK') {
        error =
            'Permission denied - please ask the user to enable it from the app settings';
      }
      print("Error = ${e}");
      getPresentLocation = null;
    }
    print('current location = ${getPresentLocation}');
    return getPresentLocation;
  }

0 个答案:

没有答案