Dart 如何使用BitmapDescriptor.fromAssetImage()设置自定义标记图标?
我正在尝试在我的Flitter应用程序中为我的地图标记设置自定义图标。由于不推荐使用Dart 如何使用BitmapDescriptor.fromAssetImage()设置自定义标记图标?,dart,flutter,google-maps-markers,Dart,Flutter,Google Maps Markers,我正在尝试在我的Flitter应用程序中为我的地图标记设置自定义图标。由于不推荐使用BitmapDescriptor.fromAsset(),我正在努力寻找如何使用BitmapDescriptor.fromAssetImage() 我没有发现任何关于这方面的文件或问题 这是我当前创建它的方式(没有自定义图像): 标记( markerId:markerId(位置toString()), 职位:pos,, 信息窗口:信息窗口( 标题:商店['store_name'], 代码段:“”, ), 图标:B
BitmapDescriptor.fromAsset()
,我正在努力寻找如何使用BitmapDescriptor.fromAssetImage()
我没有发现任何关于这方面的文件或问题
这是我当前创建它的方式(没有自定义图像):
标记(
markerId:markerId(位置toString()),
职位:pos,,
信息窗口:信息窗口(
标题:商店['store_name'],
代码段:“”,
),
图标:BitmapDescriptor.defaultMarker);
在类中定义字段:
BitmapDescriptor myIcon;
在地图准备就绪之前检索图标
@override
void initState() {
BitmapDescriptor.fromAssetImage(
ImageConfiguration(size: Size(48, 48)), 'assets/my_icon.png')
.then((onValue) {
myIcon = onValue;
});
}
设置图标:
icon: myIcon;
确保已在pubspec.yaml的颤振部分设置图标
assets:
- assets/my_icon.png
更好的解决方案是将GoogleMap小部件包装在。 在单独的将来生成标记,并指定将来的结果 到GoogleMap小部件中的标记 FutureBuilder:
FutureBuilder(
future: generateMarkers(positions),
initialData: Set.of(<Marker>[]),
builder: (context, snapshot) => GoogleMap(
initialCameraPosition: CameraPosition(target: LatLng(0, 0)),
markers: snapshot.data,
),
);
FutureBuilder(
未来:发电商(职位),
initialData:([])的集合,
生成器:(上下文,快照)=>谷歌地图(
initialCameraPosition:CameraPosition(目标:LatLng(0,0)),
标记:snapshot.data,
),
);
标记生成器:
Future<Set<Marker>> generateMarkers(List<LatLng> positions) async {
List<Marker> markers = <Marker>[];
for (final location in positions) {
final icon = await BitmapDescriptor.fromAssetImage(
ImageConfiguration(size: Size(24, 24)), 'assets/my_icon.png');
final marker = Marker(
markerId: MarkerId(location.toString()),
position: LatLng(location.latitude, location.longitude),
icon: icon,
);
markers.add(marker);
}
return markers.toSet();
}
未来生成标记(列表位置)异步{
列表标记=[];
对于(位置中的最终位置){
最终图标=等待BitmapDescriptor.fromAssetImage(
ImageConfiguration(大小:大小(24,24)),“assets/my_icon.png”);
最终标记=标记(
markerId:markerId(location.toString()),
位置:LatLng(位置、纬度、位置、经度),
图标:图标,
);
标记。添加(标记);
}
返回markers.toSet();
}
您可以像这样使用它:
Marker marker = Marker(
icon: await BitmapDescriptor.fromAssetImage(
ImageConfiguration(size: Size(48, 48)), 'assets/images/pin.png'
)
);
它起作用了!谢谢你明确的回答。标记为解决方案。大家好,在加载应用程序之前,我已经在initState中加载了图标,我与日志记录器确认了它,但在加载页面后,我看到一个错误`com.google.maps.api.android.lib6.common.apiexception.a:无法解码图像。提供的图像必须是位图。有人能帮忙吗?我将尝试使用,但返回错误。。软件说未处理的异常:PlatformException(错误,无法解码图像。提供的图像必须是位图。如何解决?感谢某些原因,我发现将
大小(48,48)
更改为大小(10,10)
不会更改图标的大小。@ImageConfiguration
的JVE999 size参数被BitmapDescriptor