Ios 颤振-如何删除单个google_maps_颤振^0.5.21标记?
自google_maps_Flatter诞生以来,有相当大的变化,这意味着移除单个标记的过程也发生了变化 我在这个问题的旧查询中发现:Ios 颤振-如何删除单个google_maps_颤振^0.5.21标记?,ios,list,flutter,dart,google-maps-markers,Ios,List,Flutter,Dart,Google Maps Markers,自google_maps_Flatter诞生以来,有相当大的变化,这意味着移除单个标记的过程也发生了变化 我在这个问题的旧查询中发现: mapController.markers.forEach((marker){ mapController.removeMarker(marker); }); 这对我不起作用,因为我得到了错误: 没有为类“GoogleMapController”定义getter“markers” 及 没有为类“GoogleMapController”定义方法“remove
mapController.markers.forEach((marker){
mapController.removeMarker(marker);
});
这对我不起作用,因为我得到了错误:
没有为类“GoogleMapController”定义getter“markers”
及
没有为类“GoogleMapController”定义方法“removeMarker”
这是我用来添加标记的:
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),
));
});
}
}
我假设在构建函数的某个地方有Google Maps小部件:
GoogleMap(
markers: _markers,
...
您只需从_markers列表中删除单个标记,并调用setState,以便使用更新的标记列表重建GoogleMap小部件
编辑-更具体地针对作者的用例:
标记ID唯一标识标记,因此您可以使用它们查找要删除的标记:
_markers.remove(_markers.firstWhere((Marker marker) => marker.markerId.value == title));
或
我以前尝试过类似于_markers.removeMarkerId“$title”的方法,但没有得到任何结果。抱歉,如果代码是错的,我是afkI假设你的意思是_markers.removeMarkermarkrid:MarkerId$title?这将不起作用,因为您正在创建一个新的标记对象,而不是引用您首先添加到列表中的同一个标记对象。如果查看标记对象的bool operator==Object other的定义,您将看到在检查两个标记对象是否相等时,标记的所有其他属性都被考虑在内,这就是为什么你的方法不起作用,因为你试图删除列表中不存在的标记。你能在回答中添加一个完整的例子吗?我没有自动取款机,所以我自己无法计算。很抱歉,我花了一段时间才回复您,您提供的答案非常有效。非常感谢!
_markers.remove(_markers.firstWhere((Marker marker) => marker.markerId == MarkerId(title)));