Flutter谷歌地图在地图渲染后添加和删除标记



嗨,我很难理解如何在地图创建后向地图添加标记(和删除旧标记(。我的应用程序有一个堆叠菜单,用户应该能够点击按钮查看其他标记。所以不知何故,我需要点击才能在地图上渲染新的标记,如果可能的话,我不想重新渲染整个地图。我知道GoogleMaps类中有一个onTap属性,但我似乎无法让它发挥作用。任何想法都将不胜感激。此外,我已经在导入的另一个类中分离了标记。这是我的相关代码:

更新我的代码!!奇怪的是,当我在下面的代码中对标记参数使用三元运算符时,它是有效的,但这还不够,我想从中选择更多。例如,我希望能够使用六个布尔标志,这些标志应该设置来自不同标记集的不同标记(例如,markr.getSpecialMarker,getBlackMarker(。你们中有人知道怎么做吗?

return Scaffold(
appBar: AppBar(
actions: [
IconButton(
icon: Icon(Icons.menu),
onPressed: () {
setState(() {
menuIsVisible = !menuIsVisible;
print(menuIsVisible.toString());
});
},
),
],
backgroundColor: Colors.black,
elevation: 1,
centerTitle: true,
title: Text(
'Title',
style: TextStyle(
color: Colors.white,
letterSpacing: 2.0,
fontSize: 40,
fontFamily: 'Bebas Neue'),
),
body: Stack(
children: <Widget>[
FutureBuilder<bool>(
//If _prepareWidget returns true we are good to go and the icons are not null
future: marker.prepareWidget(),
builder: (context, AsyncSnapshot<bool> snapshot) {
if (snapshot.hasData) {
return GoogleMapWidget(
initialCameraPosition: _initialCameraPosition,
controller: controller,

markers: markers: _saucerMarkers!
? marker.getGoldMarkers()
: marker.getBlackMarkers(),);
//onTap: () {}
//   setState(() {
//     marker.addMarkersBlack();
//   });
// });
} else {
return Center(child: CircularProgressIndicator());
}
}),
Positioned(
left: 15.0,
child: Visibility(
visible: menuIsVisible,
child: Row(
children: <Widget>[
IconButton(
icon: Icon(
Icons.change_circle_outlined,
color: changeIconColor,
size: 35.0,
),
onPressed: () {
setState(() {
if (changeIconColor == Color(0xff050505)) {
changeIconColor = Color(0xffebc030);
changeIconColorU = Color(0xff050505);
} else {
changeIconColor = Color(0xff050505);
changeIconColorU = Color(0xffebc030);
}
});
},
),
IconButton(
icon: Image.asset(
'images/image1.png',
color: changeIconColorU,
),
onPressed: () {
changeIconColorU == Color(0xff050505)
? selectedShape = iconShapeColor.UBlack
: selectedShape = iconShapeColor.UGold;
print(selectedShape);

},
),

IconButton(
icon: Image.asset(
'images/image2.png',
color: changeIconColorU,
),
onPressed: () {
changeIconColorU == Color(0xff050505)
? selectedShape = iconShapeColor.TBlack
: selectedShape = iconShapeColor.TGold;
print(selectedShape);
},
),

class GoogleMapWidget extends StatelessWidget {
const GoogleMapWidget(
{Key? key,
required CameraPosition initialCameraPosition,
required this.controller,
this.customIcon,
this.markers,
this.onTap})
: _initialCameraPosition = initialCameraPosition,
super(key: key);
final CameraPosition _initialCameraPosition;
final controller;
final BitmapDescriptor? customIcon;
final markers;
final onTap;
@override
Widget build(BuildContext context) {
return GoogleMap(
myLocationButtonEnabled: false,
zoomControlsEnabled: false,
initialCameraPosition: _initialCameraPosition,
onMapCreated: controller!,
markers: markers,
onTap: onTap,
);
}
}

我现在已经解决了我的问题!!我将把相关代码放在这里。我创建了一个新的Set _markerChoice,它是我从标记屏幕上获得的各个标记集中复制的。正如你在下面看到的,然后我只是把Set放在标记参数中(如果从一开始就为null(。当点击相应的图标时,我现在得到黑色标记或金色标记,这也可以用于更多类型的标记:我希望你能理解这个概念,尽管我不擅长解释:(

markers: markers: **_markerChoice ??
marker.getGoldMarkers());**
} else {
return Center(child: CircularProgressIndicator());
}
}),
Positioned(
left: 15.0,
child: Visibility(
visible: menuIsVisible,
child: Row(
children: <Widget>[
IconButton(
icon: Icon(
Icons.change_circle_outlined,
color: changeIconColor,
size: 35.0,
),
onPressed: () {
setState(() {
if (changeIconColor == Color(0xff050505)) {
changeIconColor = Color(0xffebc030);
changeIconColorU = Color(0xff050505);
} else {
changeIconColor = Color(0xff050505);
changeIconColorU = Color(0xffebc030);
}
});
},
),
IconButton(
icon: Image.asset(
'images/image1.png',
color: changeIconColorU,
),
onPressed: () {
**if (changeIconColorU == Color(0xff050505)) {
_markerChoice!.clear();
selectedShape = iconShapeColor.Black;
changeIconColorU = Color(0xffebc030);
for (Marker m in marker.getBlackMarkers()) {
_markerChoice!.add(m);
}
} else {
_markerChoice!.clear();
selectedShape = iconShapeColor.Gold;
changeIconColorU = Color(0xff050505);
for (Marker m in marker.getGoldMarkers()) {
_markerChoice!.add(m);**
}
}
print(selectedShape);

},
),

IconButton(
icon: Image.asset(
'images/image2.png',
color: changeIconColorU,
),
onPressed: () {
changeIconColorU == Color(0xff050505)
? selectedShape = iconShapeColor.TBlack
: selectedShape = iconShapeColor.TGold;
print(selectedShape);
},
),

相关内容

  • 没有找到相关文章

最新更新