如何使用 flutter 在用户 select 位置时更改标记

How to change Marker when user select location using flutter

我想在用户 select 某个地方时更改标记。当我设置 LatLng 硬编码时,一切都很好,但是当我尝试以编程方式更改 LatLng 时,错误显示未处理的异常:无效参数:纬度必须在 -90 到 90 度之间,但为 -119.0。

我试试这个,请检查我的代码并帮助我。这是我的代码---

LatLng latLng =new LatLng(43.761539, -123.1369);
  final menuButton = new PopupMenuButton<int>(
    onSelected: (int i) {},
    itemBuilder: (BuildContext ctx) {},
    child: new Icon(
      Icons.dashboard,
    ),
  );
  final _startPointController = TextEditingController();
  Set<Marker> markers = Set();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: Drawer(
        child: ListView(
          padding: EdgeInsets.zero,
          children: [
            DrawerHeader(
              child: Text("Header"),
            ),
            ListTile(
              title: Text("Home"),
            )
          ],
        ),
      ),
      appBar: AppBar(
        centerTitle: true,
        title: Text('Real State'),
        backgroundColor: Colors.red[700],
        actions: <Widget>[
          menuButton,
        ],
      ),
      body: Stack(
        children: <Widget>[
          FlutterMap(
            options: new MapOptions(
              center: latLng,
              zoom: 13.0,
            ),
            layers: [
              new TileLayerOptions(
                urlTemplate: "https://api.tiles.mapbox.com/v4/"
                    "{id}/{z}/{x}/{y}@2x.png?access_token={accessToken}",
                additionalOptions: {
                  'accessToken': 'pk.eyJ1Ijoic2FraWJ1bHJhc2VsIiwiYSI6ImNrODJrdWNueDBrd2Mzbm13Z2RtdGFkcHEifQ.gayvG_4Qk5uUDMaHZcyTzw',
                  'id': 'mapbox.streets',
                },
              ),
              new MarkerLayerOptions(
                markers: [
                  new Marker(
                    width: 80.0,
                    height: 80.0,
                    point: latLng,
                    builder: (ctx) =>
                    new Container(
                      child: IconButton(
                        icon: Icon(Icons.location_on),
                        color: Colors.blue,
                        iconSize: 45.0,
                        onPressed: () {
                          print('Marker tapped');
                        },
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),

          Positioned(
            top: 20.0,
            right: 15.0,
            left: 15.0,
            child: Container(
              height: 50.0,
              width: double.infinity,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(3.0),
                color: Colors.white,
                boxShadow: [
                  BoxShadow(
                      color: Colors.grey,
                      offset: Offset(1.0, 5.0),
                      blurRadius: 10,
                      spreadRadius: 3)
                ],
              ),
              child: CustomTextField(
                      hintText: "Select starting point",
                      textController: _startPointController,
                      onTap: () {
                      Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => MapBoxAutoCompleteWidget(
                      apiKey: Tokens.MAPBOX_ACCESS_TOKEN,
                      hint: "Select starting point",
                      onSelect: (place) {
                         _startPointController.text = place.placeName;
                         setState(() {
                           latLng=new LatLng(place.geometry.coordinates.first,place.geometry.coordinates.last);
                         });
//                         markers.add(
//                           Marker(
//                             width: 80.0,
//                             height: 80.0,
//                             point: LatLng(place.geometry.coordinates.first,place.geometry.coordinates.last)
//                           )
//                         );
                    },
                      limit: 10,
                          country: 'CA',
                      ),),);},
                      enabled: true,
                      ),
            ),
          ),

错误消息准确地告诉您问题所在 -- 您输入的纬度数字无效。您没有包含导致此错误的行号,但通过查看您的代码,我可以假设该行:

latLng=new LatLng(place.geometry.coordinates.first,place.geometry.coordinates.last);

负责。可能 place.geometry.coordinates.first 实际上是经度,类似地 place.geometry.coordinates.last 实际上是纬度。 LatLng 构造函数,如其名称所示,需要纬度后跟经度。经度值的范围是 -180 到 180,纬度值的范围是 -90 到 90。因此,当预期值介于 -90 到 90 之间时,你传递的值是 -119.0 表示将经度传递给纬度参数.

简而言之,尝试将上述行更改为:

latLng=new LatLng(place.geometry.coordinates.last,place.geometry.coordinates.first);