【问题标题】:Flutter google_maps_flutter can't able to change MapType by PopupMenuButtonFlutter google_maps_flutter 无法通过 PopupMenuButton 更改 MapType
【发布时间】:2019-05-16 22:36:48
【问题描述】:

我只是在玩新的Google Map Flutter 包。

我想使用PopupMenuButton 更改地图的MapType,如混合、卫星、无等。下面是我的代码。

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class MapPage extends StatefulWidget {
  @override
  _MapPageState createState() => _MapPageState();
}

class _MapPageState extends State<MapPage> {
  GoogleMapController mapController;
  List arrMapTyep = [
    {'title': 'none', 'value': MapType.none},
    {'title': 'hybrid', 'value': MapType.hybrid},
    {'title': 'normal', 'value': MapType.normal},
    {'title': 'satellite', 'value': MapType.satellite},
    {'title': 'terrain', 'value': MapType.terrain}
  ];
  MapType currentMapType = MapType.normal;

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Google Map"),
        actions: <Widget>[
          PopupMenuButton(
              icon: Icon(Icons.more_vert),
              initialValue: currentMapType,
              onSelected: (value) {
                setState(() {
                  currentMapType = value;
                });
              },
              itemBuilder: (BuildContext context) => _setupAllMapType()
              )
        ],
      ),
      body: Container(
        child: GoogleMap(
          onMapCreated: _onMapCreated,
          options: GoogleMapOptions(
              mapType: currentMapType,
              compassEnabled: true,
              myLocationEnabled: true,
              cameraPosition: CameraPosition(
                  target: LatLng(37.785834, -122.406417), 
                  zoom: 16,
                )
              ),
        ),
      ),
    );
  }

  void _onMapCreated(GoogleMapController controller) {
    setState(() {
      mapController = controller;
    });
  }

  List<PopupMenuItem> _setupAllMapType() {
    List<PopupMenuItem> temArr = List();
    for (var i = 0; i < arrMapTyep.length; i++) {
      temArr.add(
        PopupMenuItem(
          value: arrMapTyep[i]['value'],
          child: Text(arrMapTyep[i]['title']),
        ),
      );
    }

    return temArr;
  }
}

我给setState打了电话,但可以工作。

【问题讨论】:

  • 你得到解决方案了吗?

标签: google-maps dart flutter


【解决方案1】:

我不确定现在回答这个问题是否为时已晚,但万一像我这样的人偶然发现了这个问题;

MapType 的实时变化,可以使用 setState() 调用 currentMapType 变量来实现。我已经在我的应用程序中实现了这个,它像黄油一样流畅!

希望对您有所帮助。快乐飘飘!

快速参考(将法线贴图类型更改为卫星贴图类型):

   RaisedButton(
     child: Text("Satellite"),
     onPressed: () {
        setState(() {
           currentMapType = MapType.satellite;
        });
     },
   )

【讨论】:

  • 我在应用栏操作中有一个按钮,用于更改地图类型,但这样做会给我:未处理的异常:在构造函数中调用 setState() 地图是辅助小部件
猜你喜欢
  • 2019-07-15
  • 2022-10-01
  • 2019-07-30
  • 2019-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-19
  • 2023-02-18
相关资源
最近更新 更多