addMarker method
添加marker
在纬度lat
, 经度lng
的位置添加marker, 并设置标题title
和副标题snippet
, iconUri
可以是图片url, asset路径或者文件路径
Implementation
Future addMarker(
BuildContext context,
LatLng point, {
String title,
String snippet,
Uri iconUri,
}) {
Future<Uint8List> _getImageData(Uri iconUri) async {
Uint8List iconData;
switch (iconUri.scheme) {
// 网络图片
case 'https':
case 'http':
HttpClient httpClient = HttpClient();
var request = await httpClient.getUrl(iconUri);
var response = await request.close();
iconData = await consolidateHttpClientResponseBytes(response);
break;
// 文件图片
case 'file':
final imageFile = File.fromUri(iconUri);
iconData = imageFile.readAsBytesSync();
break;
// asset图片
default:
// asset的bug描述(https://github.com/flutter/flutter/issues/24865):
// android和ios平台上都取了1.0密度的图片, android上就显示了1.0密度的图片, 而ios
// 平台上使用的图片也是1.0密度, 但是根据设备密度进行了对应的放大, 导致了android和ios
// 两端的图片的大小不一致, 这里只对android根据密度选择原始图片, ios原封不动
// 这样做android端能够保证完美, ios端的话图片会有点糊, 因为原始图片是1.0密度, 但是这样
// 的话两端大小是一致的, 如果要求再高一点的话, ios这边对图片根据设备密度选择好图片后, 再进行对应密度
// 的缩小, 就是完美的了, 但是处理起来比较麻烦, 这里就不去处理了
if (Platform.isAndroid) {
final byteData = await rootBundle
.load(AmapService.toResolutionAware(context, iconUri.path));
iconData = byteData.buffer.asUint8List();
} else {
final byteData = await rootBundle.load(iconUri.path);
iconData = byteData.buffer.asUint8List();
}
break;
}
return iconData;
}
final lat = point.lat;
final lng = point.lng;
return platform(
android: (pool) async {
// 获取地图
final map = await _androidController.getMap();
// marker经纬度
final latLng = await ObjectFactory_Android
.createcom_amap_api_maps_model_LatLng__double__double(lat, lng);
// marker配置
final markerOption = await ObjectFactory_Android
.createcom_amap_api_maps_model_MarkerOptions__();
// 设置marker经纬度
await markerOption.position(latLng);
// 设置marker标题
if (title != null) {
await markerOption.title(title);
}
// 设置marker副标题
if (snippet != null) {
await markerOption.snippet(snippet);
}
// 设置marker图标
if (iconUri != null) {
Uint8List iconData = await _getImageData(iconUri);
final bitmap =
await ObjectFactory_Android.createandroid_graphics_Bitmap(
iconData);
final icon = await com_amap_api_maps_model_BitmapDescriptorFactory
.fromBitmap(bitmap);
await markerOption.icon(icon);
pool..add(bitmap)..add(icon);
}
final marker = await map.addMarker(markerOption);
pool..add(map)..add(latLng)..add(markerOption)..add(marker);
},
ios: (pool) async {
await _iosController.set_delegate(_IOSMapDelegate());
// 创建marker
final pointAnnotation =
await ObjectFactory_iOS.createMAPointAnnotation();
final coordinate =
await ObjectFactory_iOS.createCLLocationCoordinate2D(lat, lng);
// 设置经纬度
await pointAnnotation.set_coordinate(coordinate);
// 设置标题
if (title != null) {
await pointAnnotation.set_title(title);
}
// 设置副标题
if (snippet != null) {
await pointAnnotation.set_subtitle(snippet);
}
// 设置图片
// 设置marker图标
if (iconUri != null) {
Uint8List iconData = await _getImageData(iconUri);
final icon = await ObjectFactory_iOS.createUIImage(iconData);
// 由于ios端的icon参数在回调中设置, 无法在add的时候设置, 所以需要放到STACK中去
// 供ios的回调去获取
await ObjectFactory_iOS.pushStack('icon', icon);
pool..add(icon);
}
await _iosController.addAnnotation(pointAnnotation);
pool..add(pointAnnotation)..add(coordinate);
},
);
}