You need to enable JavaScript to run this app. Flutter Google Maps中如何创建自定义用户位置标记图标?
优惠活动
大模型
产品
解决方案
定价
更多

Flutter Google Maps中如何创建自定义用户位置标记图标?

Flutter 实现Google Maps自定义用户头像标记

需求效果

原代码问题

  • 仅绘制了全圆角矩形,没有实现目标样式底部的尖角标记
  • 画布、绘制区域高度硬编码为100,和传入的size参数不匹配,会出现显示比例异常
  • 头像裁剪区域未预留底部尖角位置,导致头像显示错位

修正后实现代码

import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';

Future<void> getMarkerIcon(String imagePath, Size size) async {
  final ui.PictureRecorder pictureRecorder = ui.PictureRecorder();
  final Canvas canvas = Canvas(pictureRecorder);

  final double imageRadius = size.width / 2;
  // 底部尖角高度,可根据需求自行调整
  final double arrowHeight = 20;
  // 标记总高度 = 圆形头像区域直径 + 尖角高度
  final double totalHeight = size.width + arrowHeight;

  final Paint backgroundPaint = Paint()..color = Colors.white;
  final double shadowWidth = 2.0;

  final Paint borderPaint = Paint()..color = Colors.blue; // 替换为你的主题色Utils.myColor.primary
  final double borderWidth = 3.0;

  // 绘制白色背景(上部圆形+底部尖角)
  Path backgroundPath = Path();
  backgroundPath.addRRect(RRect.fromRectAndRadius(
    Rect.fromLTWH(0, 0, size.width, size.width),
    Radius.circular(imageRadius),
  ));
  backgroundPath.moveTo(imageRadius - 10, size.width - 5);
  backgroundPath.lineTo(imageRadius, size.width + arrowHeight);
  backgroundPath.lineTo(imageRadius + 10, size.width - 5);
  backgroundPath.close();
  canvas.drawPath(backgroundPath, backgroundPaint);

  // 绘制主题色边框
  Path borderPath = Path();
  borderPath.addRRect(RRect.fromRectAndRadius(
    Rect.fromLTWH(shadowWidth, shadowWidth, size.width - shadowWidth * 2, size.width - shadowWidth * 2),
    Radius.circular(imageRadius - shadowWidth),
  ));
  borderPath.moveTo(imageRadius - 8, size.width - shadowWidth - 5);
  borderPath.lineTo(imageRadius, size.width + arrowHeight - shadowWidth * 2);
  borderPath.lineTo(imageRadius + 8, size.width - shadowWidth - 5);
  borderPath.close();
  canvas.drawPath(borderPath, borderPaint);

  // 裁剪头像圆形显示区域
  final double imageOffset = shadowWidth + borderWidth;
  Rect oval = Rect.fromLTWH(
    imageOffset,
    imageOffset,
    size.width - imageOffset * 2,
    size.width - imageOffset * 2,
  );
  canvas.clipPath(Path()..addOval(oval));

  // 加载并绘制用户头像
  ui.Image image = await getImageFromPath(imagePath);
  paintImage(canvas: canvas, image: image, rect: oval, fit: BoxFit.cover);

  // 转换为Google Maps可用的标记图标
  final ui.Image markerAsImage = await pictureRecorder.endRecording().toImage(
    size.width.toInt(),
    totalHeight.toInt(),
  );
  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 = Completer();
  ui.decodeImageFromList(imageBytes, (ui.Image img) {
    return completer.complete(img);
  });
  return completer.future;
}

调用方法

调用时传入的Size参数宽度设置为你想要的标记尺寸,高度设置为宽度+尖角高度即可,示例:

getMarkerIcon("https://你的头像url", const Size(100, 120));

内容的提问来源于stack exchange,提问作者dev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 02:54:05