我想使用 .svg 图像作为标记图标来进行标记。我正在使用 google_maps_flutter 库。使用转换成 .png 的图片可以实现,但是我找不到直接使用 .svg 文件的方法。
在阅读了这个问题之后,我在这里发布帖子:Using SVG markers in google_maps_flutter Flutter plugin 并试图使用这个线程中描述的 fromAssetImage 方法:How to change the icon size of Google Map marker in Flutter? 我希望这个库的新版本能够解决我的问题。
我尝试使用以下代码来解决它:
MapWidget 类
在阅读了这个问题之后,我在这里发布帖子:Using SVG markers in google_maps_flutter Flutter plugin 并试图使用这个线程中描述的 fromAssetImage 方法:How to change the icon size of Google Map marker in Flutter? 我希望这个库的新版本能够解决我的问题。
我尝试使用以下代码来解决它:
MapWidget 类
for (Place place in widget.places) {
place.toMarker(
context,
() => _onPlaceTapped(place),
).then(setState((){
markerSet.add
}));
}
Place类
Future<Marker> toMarker(BuildContext context, VoidCallback onTap) async {
return Marker(
markerId: MarkerId(id.toString()),
position: LatLng(lat, lon),
icon: await category.markerIcon(createLocalImageConfiguration(context)),
onTap: onTap,
);
分类类
Future<BitmapDescriptor> markerIcon(ImageConfiguration configuration) async {
BitmapDescriptor b;
switch (type) {
case CategoryType.FirstCategory:
b = await BitmapDescriptor.fromAssetImage(configuration, 'assets/markers/marker-first.svg');
break;
case CategoryType.SecondCategory:
b = await BitmapDescriptor.fromAssetImage(configuration, 'assets/markers/marker-second.svg');
break;
default:
b = await BitmapDescriptor.fromAssetImage(configuration, 'assets/markers/marker-third.svg');
break;
}
return b;
}
如果您想知道图片的路径,它已经被添加到了 pubspec.yaml 中。
我期望在地图上用自定义标记替换默认的红色 google 标记(每个自定义标记上应该有一个小图标),但是只显示了默认标记(不是 switch 语句中的默认值,我指的是 google 的默认标记)。
使用 iOS 模拟器和 google_maps_flutter 0.5.15+1 库版本没有错误信息。
drawableRoot
重命名为svgDrawableRoot
,并加上一条注释,说明picture.toPicture(32, 32)
中使用的尺寸必须小心选择,否则 SVG 图像将无法完全显示。 - wenolOaR