我使用谷歌Map显示用户的位置。我需要一个自定义标记显示用户的位置。用户图像是从URL获取。但我不能像在www.example.com代码所示的形状image.my标记如下。与我的代码,我不能实现所需的背景形状。谁来帮帮我。
Future<void> getMarkerIcon(String imagePath, Size size) async {
final ui.PictureRecorder pictureRecorder = ui.PictureRecorder();
final Canvas canvas = Canvas(pictureRecorder);
final Radius radius = Radius.circular(size.width / 2);
final Paint tagPaint = Paint()..color = Utils.myColor.primary;
final double tagWidth = 20.0;
final Paint shadowPaint = Paint()..color = Colors.white;
final double shadowWidth = 2.0;
final Paint borderPaint = Paint()..color = Colors.white;
final double borderWidth = 2.0;
final double imageOffset = shadowWidth + borderWidth;
// Add shadow circle
canvas.drawRRect(
RRect.fromRectAndCorners(
Rect.fromLTWH(0.0, 0.0, size.width, 100),
topLeft: radius,
topRight: radius,
bottomLeft: radius,
bottomRight: radius,
),
shadowPaint);
// Add border circle
canvas.drawRRect(
RRect.fromRectAndCorners(
Rect.fromLTWH(
shadowWidth, shadowWidth, size.width - (shadowWidth * 2), 100),
topLeft: radius,
topRight: radius,
bottomLeft: radius,
bottomRight: radius,
),
borderPaint);
// Oval for the image
Rect oval = Rect.fromLTWH(imageOffset, imageOffset,
size.width - (imageOffset * 2), size.height - (imageOffset * 2));
// Add path for oval image
canvas.clipPath(Path()..addOval(oval));
// Add image
ui.Image image = await getImageFromPath(
imagePath); // Alternatively use your own method to get the image
paintImage(canvas: canvas, image: image, rect: oval, fit: BoxFit.fitWidth);
// Convert canvas to image
final ui.Image markerAsImage =
await pictureRecorder.endRecording().toImage(size.width.toInt(), 100);
// Convert image to bytes
final ByteData byteData =
await markerAsImage.toByteData(format: ui.ImageByteFormat.png);
final Uint8List uint8List = byteData.buffer.asUint8List();
setState(() {
markerIcon = BitmapDescriptor.fromBytes(uint8List);
});
}
Future<ui.Image> getImageFromPath(String imagePath) async {
File imageFile = await DefaultCacheManager().getSingleFile(imagePath);
Uint8List imageBytes = imageFile.readAsBytesSync();
final Completer<ui.Image> completer = new Completer();
ui.decodeImageFromList(imageBytes, (ui.Image img) {
return completer.complete(img);
});
return completer.future;
}
4条答案
按热度按时间lokaqttq1#
检查https://pub.dev/packages/custom_map_markers以将任何Widget渲染为标记。使用小部件可能更容易实现您想要的特定外观
vkc1a9a22#
sdnqo3pr3#
你可以尝试将widget作为标记,说明如下:
custom map marker
已更新
add WidgetsBinding.instance.addPostFrameCallback
41ik7eoe4#
尝试使用这个在线工具。这些对我帮助很大。shapemaker.web.app