颤振 - 如何删除单个google_maps_flutter ^0.5.21 标记?



google_maps_flutter成立以来发生了相当大的变化,这意味着删除单个标记的过程也发生了变化。

我在这个问题的旧查询中发现的是,删除版本 ~ 0.0.1 上的标记,是这样的:

mapController.markers.forEach((marker){
mapController.removeMarker(marker);
});

这对我来说不起作用,因为我收到错误:

The getter 'markers' isn't defined for the class 'GoogleMapController'.

The method 'removeMarker' isn't defined for the class 'GoogleMapController'.

这是我用来添加标记的:

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地图小部件:

GoogleMap(
markers: _markers,
...

您只需从_markers列表中删除单个Marker并调用setState,以便使用更新的标记列表重建GoogleMap小部件。

编辑 - 更具体地针对作者的用例:

标记 Id 唯一标识标记,因此您可以使用它们来查找要删除的标记:

_markers.remove(_markers.firstWhere((Marker marker) => marker.markerId.value == title));

_markers.remove(_markers.firstWhere((Marker marker) => marker.markerId == MarkerId(title)));

相关内容

  • 没有找到相关文章

最新更新