wen*_*OaR 5 svg google-maps-markers flutter
我希望使用.svg图像作为图标放置标记。我正在使用google_maps_flutter库。这适用于.png转换后的图像,但我找不到直接使用.svg文件的方法。
阅读此问题后,我将在此发布:在google_maps_flutter Flutter插件中使用SVG标记
并尝试使用此线程中所述的fromAssetImage方法:如何在Flutter中更改Google Map标记的图标大小?
我希望这个新版本的库可以帮助解决我的问题。
我尝试使用以下代码解决它:
MapWidget类
for (Place place in widget.places) {
place.toMarker(
context,
() => _onPlaceTapped(place),
).then(setState((){
markerSet.add
}));
}
Run Code Online (Sandbox Code Playgroud)
地方课程
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,
);
Run Code Online (Sandbox Code Playgroud)
类别类
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;
}
Run Code Online (Sandbox Code Playgroud)
如果您想知道,图像的路径已经添加到pubspec.yaml中。
我希望默认的红色Google标记会被地图上的一些自定义标记(每个自定义标记上都应该有一个小图标图像)代替,但是只显示默认标记(我所说的不是switch语句中的默认标记)谷歌的默认标记)。
使用iOS模拟器和google_maps_flutter 0.5.15 + 1库版本没有错误消息。
您可以在这里找到这个问题的答案:
在 google_maps_flutter Flutter 插件中使用 SVG 标记
我尝试了 rednuht 的答案,它很有魅力。
编辑(回顾)
答案需要使用 flutter_svg 插件(^0.13 或 0.14.1 似乎对我自己有用)。
首先,创建一个函数,该函数将为您提供资产中的 BitmapDescriptor。确保该资产已在您的 pubspec 文件中声明。
import 'dart:ui' as ui;
import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart';
Future<BitmapDescriptor> _bitmapDescriptorFromSvgAsset(BuildContext context, String assetName) async {
String svgString = await DefaultAssetBundle.of(context).loadString(assetName);
//Draws string representation of svg to DrawableRoot
DrawableRoot svgDrawableRoot = await svg.fromSvgString(svgString, null);
ui.Picture picture = svgDrawableRoot.toPicture();
ui.Image image = await picture.toImage(26, 37);
ByteData bytes = await image.toByteData(format: ui.ImageByteFormat.png);
return BitmapDescriptor.fromBytes(bytes.buffer.asUint8List());
}
Run Code Online (Sandbox Code Playgroud)
我在应用程序开始时实例化了一个 Map 对象,以便我可以使用 BitmapDescriptor,而无需使用大量 async/await 调用。
数据模型.dart
Map<CategoryType, BitmapDescriptor> descriptors = Map<Category, BitmapDescriptor>();
Future<void> loadBitmapDescriptors(context) async {
descriptors[CategoryType.FirstCatgory] = await _bitmapDescriptorFromSvgAsset(context, 'assets/markers/marker-first.svg');
descriptors[CategoryType.SecondCategory] = await _bitmapDescriptorFromSvgAsset(context, 'assets/markers/marker-second.svg');
descriptors[CategoryType.ThirdCategory] = await _bitmapDescriptorFromSvgAsset(context, 'assets/markers/marker-third.svg');
}
Run Code Online (Sandbox Code Playgroud)
主程序.dart
DataModel.of(context).loadBitmapDescriptors(context);
Run Code Online (Sandbox Code Playgroud)
所以之后我所要做的就是在需要时正确调用它。
Marker marker = Marker(markerId: MarkerId(id.toString()), icon: descriptors[category], position: LatLng(lat, lon));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
393 次 |
| 最近记录: |