首页 > 解决方案 > 如何使用 GoogleMapController 更改 MapType?

问题描述

我想用 GoogleMapController 改变 MapType。我使用 floatingActionButtom 来改变它。GoolgeMapOptions 已从 GoogleMap 小部件中删除,我无法使用此属性。现在我不知道改用什么代码。我想通过 onMapType 函数更改 Maptype(从普通到卫星),但我不能在新版本的 GoogleMap 中使用 updateMapOptions 和 GoolgeMapOptions。

class MapScreen extends StatefulWidget{
  @override
  State<StatefulWidget> createState() => new MapScreenState();
}

class MapScreenState extends State<MapScreen>{
  GoogleMapController mapController;
  MapType currentMaptype=MapType.normal;
  final myGoogleMap=GlobalKey();

  void _onMapController(GoogleMapController controller){
    mapController=controller;
  }
  void _onMaptype(){
    if(currentMaptype==MapType.normal){
     mapController.updateMapOptions(
        GoogleMapOptions(mapType: MapType.satellite);
      currentMaptype=MapType.satellite;
    }
    else{
      mapController.updateMapOptions(
        GoogleMapOptions(mapType: MapType.normal);
      currentMaptype=MapType.normal;
    }
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return   new Scaffold(
      body: new Stack(
        alignment: Alignment.bottomCenter,
        children: <Widget>[
          new GoogleMap(
            initialCameraPosition:CameraPosition(
            target: LatLng(85.254, 74.542)) ,
            onMapCreated:_onMapController,
            mapType: currentMaptype,
          ),
          new Align(
            alignment: Alignment.bottomLeft,
            child: new Padding(
              padding: EdgeInsets.only(bottom: 25, left: 8) ,
              child: new Column(
                mainAxisAlignment: MainAxisAlignment.end,
                children: <Widget>[
                  new FloatingActionButton(
                    onPressed: _onMaptype,
                    backgroundColor: Colors.green,
                    child: new Icon(Icons.map, color: Colors.white,size: 30),
                  ),
                ],
              ),
            ),
          )
        ],
      ),
    );
  }
  }

标签: flutter

解决方案


推荐阅读