更改标记后,Flutter Google 地图不会重新加载
我正在使用谷歌地图小部件,当我第一次使用静态标记渲染地图时,它会显示所有标记,但是当我从 API 获取数据并尝试向地图添加新标记时,它不会显示它们。
在我正在使用的代码下面:
class Map extends StatefulWidget {
final Position initialPosition;
final List<Marker> allSitesMarkers;
final List<Site> placesSite;
final List<String> mapMarkersCategories;
const Map(this.initialPosition, this.allSitesMarkers, this.placesSite,
this.mapMarkersCategories,
{Key key})
: super(key: key);
@override
State<StatefulWidget> createState() => MapState();
}
class MapState extends State<Map> {
Completer<GoogleMapController> _controller = Completer();
String currentCategoryFilter;
bool filtred = false;
Set<Marker> _markers = Set();
List<Marker> markersToShow;
@override
void initState() {
super.initState();
refreshMarkers();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: (widget.placesSite.isNotEmpty)
? Stack(
children: [
GoogleMap(
initialCameraPosition: const CameraPosition(
target: LatLng(36.798, 10.1717), zoom: 15),
mapType: MapType.normal,
myLocationEnabled: true,
markers: _markers,
onMapCreated: (GoogleMapController controller) {
controller.setMapStyle(
'[ //... ]');
_controller.complete(controller);
},
),
_chips()
],
)
: const NoListMap(),
);
}
Widget _chips() {
//..
}
filterSites(String category) {
setState(() {
_markers.clear();
filtred = true;
markersToShow = widget.allSitesMarkers
.where((element) => element.infoWindow.title == category)
.toList();
});
refreshMarkers();
}
refreshMarkers() {
if (filtred) {
setState(() {
_markers.clear();
_markers.addAll(markersToShow);
});
} else {
setState(() {
_markers.clear();
_markers.addAll(widget.allSitesMarkers);
});
}
}
}
我正在使用:
颤振2.10.2
飞镖2.16.1
谷歌地图颤振:2.0.9
I'm working with the google map widget, when I render the map for the first time with static markers it displays all the markers, but when I get the data from API and try to add new markers to the map, it won't display them.
below the code that i'm using :
class Map extends StatefulWidget {
final Position initialPosition;
final List<Marker> allSitesMarkers;
final List<Site> placesSite;
final List<String> mapMarkersCategories;
const Map(this.initialPosition, this.allSitesMarkers, this.placesSite,
this.mapMarkersCategories,
{Key key})
: super(key: key);
@override
State<StatefulWidget> createState() => MapState();
}
class MapState extends State<Map> {
Completer<GoogleMapController> _controller = Completer();
String currentCategoryFilter;
bool filtred = false;
Set<Marker> _markers = Set();
List<Marker> markersToShow;
@override
void initState() {
super.initState();
refreshMarkers();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: (widget.placesSite.isNotEmpty)
? Stack(
children: [
GoogleMap(
initialCameraPosition: const CameraPosition(
target: LatLng(36.798, 10.1717), zoom: 15),
mapType: MapType.normal,
myLocationEnabled: true,
markers: _markers,
onMapCreated: (GoogleMapController controller) {
controller.setMapStyle(
'[ //... ]');
_controller.complete(controller);
},
),
_chips()
],
)
: const NoListMap(),
);
}
Widget _chips() {
//..
}
filterSites(String category) {
setState(() {
_markers.clear();
filtred = true;
markersToShow = widget.allSitesMarkers
.where((element) => element.infoWindow.title == category)
.toList();
});
refreshMarkers();
}
refreshMarkers() {
if (filtred) {
setState(() {
_markers.clear();
_markers.addAll(markersToShow);
});
} else {
setState(() {
_markers.clear();
_markers.addAll(widget.allSitesMarkers);
});
}
}
}
I'm using:
Flutter 2.10.2
Dart 2.16.1
google_maps_flutter : 2.0.9
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
_markers
是一个Set
,在setState
中,您使用clear
和addAll
来清空它并向集合中添加新项目。但是,当您这样做时,_markers
的值不会改变,因此不会触发重建。发生这种情况是因为
_markers
保存了对Set
的引用(如列表、对象等)。您需要为
_markers
分配一个新值来触发重建,例如:这实际上会更改
_markers
中保存的引用。Your
_markers
is aSet
, and insetState
you useclear
andaddAll
to empty it and to add new items to the set. But the value of_markers
does not change when you do so, therefore it will not trigger a rebuild.This happens because
_markers
holds a reference to theSet
(like lists, objects etc.).You need to assign a new value to
_markers
to trigger a rebuild, for example:This will actually change the reference kept in
_markers
.这就是我更新职位的方法
This is what I Did to update the posistion