问题描述
自google_maps_flutter
诞生以来,情况已经发生了一些变化,这意味着删除单个标记的过程也发生了变化.
Quite a bit has changed since the inception of google_maps_flutter
, this means that the process for removing individual markers has also changed.
我在较早的查询中发现的问题,删除版本上的标记〜 0.0.1 ,是这样的:
What I've found on older queries for this question, removing markers on version ~ 0.0.1, is this:
mapController.markers.forEach((marker){
mapController.removeMarker(marker);
});
这对我不起作用,因为出现错误:
This doesn't work for me as I get the errors:
The getter 'markers' isn't defined for the class 'GoogleMapController'.
和
The method 'removeMarker' isn't defined for the class 'GoogleMapController'.
这是我用来添加标记的方法:
This is what I use to add a marker:
void _addMarker(LatLng latlang, String title) {
var _width = MediaQuery.of(context).size.width;
var _height = MediaQuery.of(context).size.height;
double _topHeight = 55;
if (_height == 896.0 && _width == 414.0) {
_topHeight = 83;
} else if (_height == 812.0 && _width == 375.0) {
_topHeight = 78;
}
double mapsettingsHeight = 225;
if (_topHeight == 55){
mapsettingsHeight = 225;
} else {
mapsettingsHeight = 255;
}
if (_markers.contains(title)) {
print("error");
} else {
setState(() {
_markers.add(Marker(
markerId: MarkerId(title),
position: latlang,
infoWindow: InfoWindow(
title: title,
snippet: title,
onTap: () {
showCupertinoModalPopup(
context: context,
builder: (BuildContext context) {
return Material(
child: Container(
width: _width,
height: mapsettingsHeight,
decoration: BoxDecoration(
color: Color(0xFFF9F9F9).withAlpha(200),
borderRadius: BorderRadius.only(topLeft:Radius.circular(10),topRight:Radius.circular(10))
),
child: Column(
children: <Widget>[
Container(
width: _width,
height: 40,
padding: EdgeInsets.all(10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
height: 20,
alignment: Alignment.center,
child: Text(title,style:TextStyle(fontFamily:'Helvetica',fontSize:15,color:Colors.black,fontWeight:FontWeight.w600))
),
GestureDetector(
child: Container(
width: 50,
height: 50,
alignment: Alignment.topRight,
child: Icon(Icons.keyboard_arrow_down,size:25,color:Colors.black.withAlpha(100)),
),
onTap: () {
Navigator.pop(context);
}
)
],
),
),
_customDivider(),
Divider(height:10,color:Colors.transparent),
Container(
width: _width,
height: 12.5,
padding: EdgeInsets.only(left:10,right:10),
child: GestureDetector(
child: Text("Edit Marker",style: TextStyle(fontFamily:'Helvetica',fontSize:12.5,color:Colors.blue,fontWeight:FontWeight.w400)),
onTap: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: TextField(
controller: _locationMarkerTitle,
decoration: InputDecoration(
hintText: 'Name this location',
hintStyle: TextStyle(fontFamily:'Helvetica',fontSize:15,color:Colors.black,fontWeight:FontWeight.w200),
),
),
titlePadding: EdgeInsets.all(10),
content: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
GestureDetector(
child: Text("Submit (this doesn't work!)",style: TextStyle(fontFamily:'Helvetica',fontSize:15,color:Colors.blue,fontWeight:FontWeight.w200)),
onTap: () {
_addMarker(LatLng(_position.latitude,_position.longitude),_locationMarkerTitle.text);
Navigator.pop(context);
}
),
],
)
);
}
);
}
),
),
Divider(height:10,color:Colors.transparent),
Container(
width: _width,
height: 12.5,
padding: EdgeInsets.only(left:10,right:10),
child: GestureDetector(
child: Text("Remove Marker",style: TextStyle(fontFamily:'Helvetica',fontSize:12.5,color:Colors.blue,fontWeight:FontWeight.w400)),
onTap: () {
setState(() {
//where the solution go
});
}
),
),
],
),
),
);
}
);
}
),
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueAzure),
));
});
}
}
推荐答案
我假定您在build()
函数的某个位置具有Google Maps小部件:
I assume somewhere in your build()
function you have the Google Maps widget:
GoogleMap(
markers: _markers,
...
您只需要从_markers
列表中删除单个Marker
,然后调用setState
,以便使用更新的标记列表重建GoogleMap
小部件.
You simply have to remove the individual Marker
from your _markers
list and invoke setState
so that the GoogleMap
widget is rebuilt with the updated list of markers.
编辑-更具体地针对作者的用例:
Edit - to be more specific to the author's use case:
MarkerIds唯一地标识一个标记,因此您可以使用这些标记来查找要删除的标记:
MarkerIds uniquely identify a Marker, so you can use those to find the Marker you want to remove:
_markers.remove(_markers.firstWhere((Marker marker) => marker.markerId.value == title));
或
_markers.remove(_markers.firstWhere((Marker marker) => marker.markerId == MarkerId(title)));
这篇关于Flutter-如何删除单个google_maps_flutter ^ 0.5.21标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!