在我的应用程序中,我需要在地图加载后立即显示交付的位置及其相应的名称。我已经从这个问题中尝试了这种方法,但标记没有加载(但如果我没有使用我之前链接的代码中使用的自定义标记,它会加载(。我哪里错了?
对于自定义标记:
static Future<Uint8List> create(String deliveryName) async {
final ui.PictureRecorder pictureRecorder = ui.PictureRecorder();
final Canvas canvas = Canvas(pictureRecorder);
final Paint paint1 = Paint()..color = Colors.grey;
final int size = 100;
canvas.drawCircle(Offset(size / 2, size / 2), size / 2.0, paint1);
TextPainter painter = TextPainter(textDirection: TextDirection.ltr);
painter.text = TextSpan(
text:
deliveryName,
style: TextStyle(
fontSize: size / 4, color: Colors.black, fontWeight: FontWeight.bold),
);
painter.layout();
painter.paint(
canvas,
Offset(size / 2 - painter.width / 2, size / 2 - painter.height / 2),
);
final img = await pictureRecorder.endRecording().toImage(size, size);
final data = await img.toByteData(format: ui.ImageByteFormat.png);
return data!.buffer.asUint8List();
}
用途:
void setMarkers() async{
_markers.clear();
print('hi');
for (Delivery delivery in _deliveries) {
_markers.add(Marker(
markerId: MarkerId(delivery.name),
position: LatLng(delivery.latitude, delivery.longitude),
icon: BitmapDescriptor.fromBytes(await CustomMarker.create(delivery.name))
));
}
}
这是一个示例代码,用于在谷歌地图上的自定义标记中显示动态变化的文本。
class MyMap extends StatefulWidget {
@override
_MyMapState createState() => _MyMapState();
}
class _MyMapState extends State<MyMap> {
String dynamicText = "Hello World";
LatLng position = LatLng(37.4219999, -122.0840575);
Marker marker = Marker(
markerId: MarkerId("1"),
position: LatLng(37.4219999, -122.0840575),
icon: BitmapDescriptor.fromAssetString(
"assets/custom_marker_icon.png",
text: "Hello World",
),
);
void updateDynamicText(String newDynamicText) {
setState(() {
dynamicText = newDynamicText;
marker = Marker(
markerId: MarkerId("1"),
position: position,
icon: BitmapDescriptor.fromAssetString(
"assets/custom_marker_icon.png",
text: dynamicText,
),
);
});
}
@override
Widget build(BuildContext context) {
return GoogleMap(
markers: Set<Marker>.of([marker]),
initialCameraPosition: CameraPosition(
target: position,
zoom: 15.0,
),
onMapCreated: (GoogleMapController controller) {},
onTap: (LatLng newPosition) {
updateDynamicText("New Text");
},
);
}
}