在此示例中,我将分享如何在google_maps_flutter Flutter插件中使用SVG标记。首先,创建一个函数,该函数将为您提供资产中的BitmapDescriptor。确保在pubspec文件中声明了资产。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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());
}
|
我在应用程序的开头实例化了一个Map对象,这样我就可以使用BitmapDescriptors而不用使用很多异步/等待调用。
data_model.dart
1
2
3
4
5
6
7
|
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’);
}
|
主镖
1
|
DataModel.of(context).loadBitmapDescriptors(context);
|
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/261956.html