拓展插件支持

This commit is contained in:
Kuloud
2023-12-30 16:42:18 +08:00
parent 5abe4d7f6d
commit 8c2b86724b
15 changed files with 278 additions and 102 deletions

View File

@ -18,3 +18,4 @@ export 'package:amap_map/src/types/types.dart';
part 'src/amap_initializer.dart';
part 'src/amap_controller.dart';
part 'src/amap_widget.dart';
part 'src/amap_loader.dart';

View File

@ -115,34 +115,11 @@ class AMapController {
mapId: mapId, animated: animated, duration: duration);
}
///设置地图每秒渲染的帧数
Future<void> setRenderFps(int fps) {
return _methodChannel.setRenderFps(fps, mapId: mapId);
}
///地图截屏
Future<Uint8List?> takeSnapshot() {
return _methodChannel.takeSnapshot(mapId: mapId);
}
/// 获取地图审图号(普通地图)
///
/// 任何使用高德地图API调用地图服务的应用必须在其应用中对外透出审图号
///
/// 如高德地图在"关于"中体现
Future<String?> getMapContentApprovalNumber() {
return _methodChannel.getMapContentApprovalNumber(mapId: mapId);
}
/// 获取地图审图号(卫星地图)
///
/// 任何使用高德地图API调用地图服务的应用必须在其应用中对外透出审图号
///
/// 如高德地图在"关于"中体现
Future<String?> getSatelliteImageApprovalNumber() {
return _methodChannel.getSatelliteImageApprovalNumber(mapId: mapId);
}
/// 清空缓存
Future<void> clearDisk() {
return _methodChannel.clearDisk(mapId: mapId);

58
lib/src/amap_loader.dart Normal file
View File

@ -0,0 +1,58 @@
part of amap_map;
class AMapLoader extends StatefulWidget {
const AMapLoader(
{super.key, required this.mapView, required this.extensions});
final Widget mapView;
final List<AMapExtension> extensions;
@override
State<AMapLoader> createState() => _AMapLoaderState();
static void prepare() {
// TODO loop handle with extensions
}
Widget buildFromExtension(AMapContext aMapContext) {
// TODO loop handle with extensions
return mapView;
}
void prepareFromExtension(AMapContext aMapContext) {
for (var e in extensions) {
e.prepare(aMapContext);
}
}
}
class _AMapLoaderState extends State<AMapLoader> {
@override
void didChangeDependencies() {
final aMapContext = AMapContext(
buildContext: context,
currentStep: CurrentStep.preparing,
loader: widget);
widget.prepareFromExtension(aMapContext);
super.didChangeDependencies();
}
@override
void dispose() {
for (var e in widget.extensions) {
e.onDispose();
}
super.dispose();
}
@override
Widget build(BuildContext context) {
// Set the extension context for this node.
final aMapContext = AMapContext(
buildContext: context,
currentStep: CurrentStep.building,
loader: widget);
return widget.buildFromExtension(aMapContext);
}
}

View File

@ -97,6 +97,8 @@ class AMapWidget extends StatefulWidget {
///需要应用到地图上的手势集合
final Set<Factory<OneSequenceGestureRecognizer>> gestureRecognizers;
final List<AMapExtension> extensions;
/// 创建一个展示高德地图的widget
///
/// 在app首次启动时必须传入高德合规声明配置[privacyStatement],后续如果没有变化不需要重复设置
@ -108,37 +110,38 @@ class AMapWidget extends StatefulWidget {
/// 高德SDK合规使用方案请参考https://lbs.amap.com/news/sdkhgsy
///
/// [AssertionError] will be thrown if [initialCameraPosition] is null;
const AMapWidget({
Key? key,
this.initialCameraPosition =
const CameraPosition(target: LatLng(39.909187, 116.397451), zoom: 10),
this.mapType = MapType.normal,
this.buildingsEnabled = true,
this.compassEnabled = false,
this.labelsEnabled = true,
this.limitBounds,
this.minMaxZoomPreference,
this.rotateGesturesEnabled = true,
this.scaleEnabled = true,
this.scrollGesturesEnabled = true,
this.tiltGesturesEnabled = true,
this.touchPoiEnabled = true,
this.trafficEnabled = false,
this.zoomGesturesEnabled = true,
this.onMapCreated,
this.gestureRecognizers = const <Factory<OneSequenceGestureRecognizer>>{},
this.customStyleOptions,
this.myLocationStyleOptions,
this.onCameraMove,
this.onCameraMoveEnd,
this.onLocationChanged,
this.onTap,
this.onLongPress,
this.onPoiTouched,
this.markers = const <Marker>{},
this.polylines = const <Polyline>{},
this.polygons = const <Polygon>{},
}) : super(key: key);
const AMapWidget(
{Key? key,
this.initialCameraPosition =
const CameraPosition(target: LatLng(39.909187, 116.397451), zoom: 10),
this.mapType = MapType.normal,
this.buildingsEnabled = true,
this.compassEnabled = false,
this.labelsEnabled = true,
this.limitBounds,
this.minMaxZoomPreference,
this.rotateGesturesEnabled = true,
this.scaleEnabled = true,
this.scrollGesturesEnabled = true,
this.tiltGesturesEnabled = true,
this.touchPoiEnabled = true,
this.trafficEnabled = false,
this.zoomGesturesEnabled = true,
this.onMapCreated,
this.gestureRecognizers = const <Factory<OneSequenceGestureRecognizer>>{},
this.customStyleOptions,
this.myLocationStyleOptions,
this.onCameraMove,
this.onCameraMoveEnd,
this.onLocationChanged,
this.onTap,
this.onLongPress,
this.onPoiTouched,
this.markers = const <Marker>{},
this.polylines = const <Polyline>{},
this.polygons = const <Polygon>{},
this.extensions = const []})
: super(key: key);
///
@override
@ -149,6 +152,7 @@ class _MapState extends State<AMapWidget> {
Map<String, Marker> _markers = <String, Marker>{};
Map<String, Polyline> _polylines = <String, Polyline>{};
Map<String, Polygon> _polygons = <String, Polygon>{};
Map<String, AMapExtension> _extensions = <String, AMapExtension>{};
final Completer<AMapController> _controller = Completer<AMapController>();
late _AMapOptions _mapOptions;
@ -169,16 +173,23 @@ class _MapState extends State<AMapWidget> {
widget.gestureRecognizers,
onPlatformViewCreated,
);
return mapView;
return AMapLoader(
mapView: mapView,
extensions: widget.extensions,
);
}
@override
void initState() {
AMapLoader.prepare();
super.initState();
_mapOptions = _AMapOptions.fromWidget(widget);
_markers = keyByMarkerId(widget.markers);
_polygons = keyByPolygonId(widget.polygons);
_polylines = keyByPolylineId(widget.polylines);
_extensions = keyByExtensionId(widget.extensions);
print('initState AMapWidget');
}
@ -218,6 +229,12 @@ class _MapState extends State<AMapWidget> {
this,
);
_controller.complete(controller);
if (_extensions.isNotEmpty) {
await Future.forEach(
_extensions.values, (e) => e.bindMethodChannel(controller.channel));
}
final MapCreatedCallback? _onMapCreated = widget.onMapCreated;
if (_onMapCreated != null) {
_onMapCreated(controller);

View File

@ -235,14 +235,6 @@ class MethodChannelAMapFlutterMap implements AMapFlutterPlatform {
});
}
///设置地图每秒渲染的帧数
Future<void> setRenderFps(int fps, {required int mapId}) {
return channel(mapId)
.invokeMethod<void>('map#setRenderFps', <String, dynamic>{
'fps': fps,
});
}
///截屏
Future<Uint8List?> takeSnapshot({
required int mapId,
@ -250,21 +242,6 @@ class MethodChannelAMapFlutterMap implements AMapFlutterPlatform {
return channel(mapId).invokeMethod<Uint8List>('map#takeSnapshot');
}
//获取地图审图号(普通地图)
Future<String?> getMapContentApprovalNumber({
required int mapId,
}) {
return channel(mapId).invokeMethod<String>('map#contentApprovalNumber');
}
//获取地图审图号(卫星地图)
Future<String?> getSatelliteImageApprovalNumber({
required int mapId,
}) {
return channel(mapId)
.invokeMethod<String>('map#satelliteImageApprovalNumber');
}
Future<void> clearDisk({
required int mapId,
}) {

View File

@ -0,0 +1,30 @@
import 'package:flutter/services.dart';
import 'types.dart';
abstract class AMapExtension {
late String _id;
String get id => _id;
AMapExtension() {
this._id = this.hashCode.toString();
}
void prepare(AMapContext aMapContext) {}
void onDispose() {}
bindMethodChannel(MethodChannel channel) {}
}
Map<String, AMapExtension> keyByExtensionId(
Iterable<AMapExtension> extensions) {
// ignore: unnecessary_null_comparison
if (extensions == null) {
return <String, AMapExtension>{};
}
return Map<String, AMapExtension>.fromEntries(extensions.map(
(AMapExtension extension) =>
MapEntry<String, AMapExtension>(extension.id, extension)));
}

View File

@ -0,0 +1,18 @@
import 'package:amap_map/amap_map.dart';
import 'package:flutter/material.dart';
class AMapContext {
final BuildContext buildContext;
final CurrentStep currentStep;
final AMapLoader loader;
AMapContext(
{required this.buildContext,
required this.currentStep,
required this.loader});
}
enum CurrentStep {
preparing,
building,
}

View File

@ -8,3 +8,5 @@ export 'polyline_updates.dart';
export 'polygon.dart';
export 'polygon_updates.dart';
export 'bitmap.dart';
export 'extension_context.dart';
export 'amap_extension.dart';