[英]Flutter google_maps_flutter can't able to change MapType by PopupMenuButton
[英]Flutter Google Maps: How can you change maptype after runtime?
当按下如下所示的按钮时,我试图从normal
更改为satellite
,但我收到一个错误,即 setMapType 不存在。
mapController.setMapType(MapType.satellite);
任何人都知道我做错了什么?
创建一个 MapType 变量:
MapType _currentMapType = MapType.normal;
在调用您的 Google Map 小部件时引用此变量:
googleMap = new GoogleMap( mapType: _currentMapType, //etc
创建一个浮动按钮小部件来切换 map 类型:
floatingActionButton: FloatingActionButton( child: Icon(Icons.layers), onPressed: ()=> { setState(() { _currentMapType = (_currentMapType == MapType.normal)? MapType.satellite: MapType.normal; }); }, heroTag: null, ),
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:search_map_place/search_map_place.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
String apiKEY;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Search Map Place Demo',
home: MapSample(),
);
}
}
class MapSample extends StatefulWidget {
@override
State<MapSample> createState() => MapSampleState();
}
class MapSampleState extends State<MapSample> {
Completer<GoogleMapController> _mapController = Completer();
final CameraPosition _initialCamera = CameraPosition(
target: LatLng(-20.3000, -40.2990),
zoom: 14.0000,
);
var maptype = MapType.normal;
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
body: Stack(
children: <Widget>[
GoogleMap(
mapType: maptype,
initialCameraPosition: _initialCamera,
onMapCreated: (GoogleMapController controller) {
_mapController.complete(controller);
},
),
],
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.white,
foregroundColor: Colors.black,
child: const Icon(Icons.my_location),
onPressed: () {
setState(() {
this.maptype=MapType.satellite;
});
},
),
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.