|
|
@@ -1,3 +1,5 @@
|
|
|
+import 'dart:async';
|
|
|
+
|
|
|
import 'package:flutter/services.dart';
|
|
|
import '../../flutter_map.dart';
|
|
|
import 'map_platform.dart';
|
|
|
@@ -6,38 +8,47 @@ class FlutterMap {
|
|
|
FlutterMap._();
|
|
|
|
|
|
static MapPlatform? _mapPlatform;
|
|
|
+ static Completer<void>? _initCompleter;
|
|
|
+
|
|
|
+ static Future<void> init() {
|
|
|
+ if (_mapPlatform != null) return Future.value();
|
|
|
+
|
|
|
+ if (_initCompleter != null) return _initCompleter!.future;
|
|
|
+
|
|
|
+ _initCompleter = Completer<void>();
|
|
|
+
|
|
|
+ const MethodChannel channel = MethodChannel(MapConstants.mapMethodChannel);
|
|
|
+ final MapPlatform mapPlatform = MapPlatformImpl(channel);
|
|
|
+
|
|
|
+ mapPlatform.init().then((ok) {
|
|
|
+ if (!ok) throw Exception("地图初始化失败,未找到适配的地图,请检查是否添加地图库");
|
|
|
+ _mapPlatform = mapPlatform;
|
|
|
+ _initCompleter?.complete();
|
|
|
+ }).catchError((e, stack) {
|
|
|
+ _initCompleter?.completeError(e, stack);
|
|
|
+ });
|
|
|
|
|
|
- static Future<void> init() async {
|
|
|
- if (_mapPlatform != null) {
|
|
|
- return;
|
|
|
- }
|
|
|
- MethodChannel channel = const MethodChannel(MapConstants.mapMethodChannel);
|
|
|
-
|
|
|
- MapPlatform mapPlatform = MapPlatformImpl(channel);
|
|
|
- if (!await mapPlatform.init()) {
|
|
|
- throw Exception("地图初始化失败,未找到适配的地图,请检查是否添加地图库");
|
|
|
- }
|
|
|
- _mapPlatform = mapPlatform;
|
|
|
+ return _initCompleter!.future;
|
|
|
}
|
|
|
|
|
|
static Future<String?> getPlatformName() {
|
|
|
- assert(_mapPlatform != null, "请先初始化地图");
|
|
|
+ assert(_mapPlatform != null, "请先调用 FlutterMap.init()");
|
|
|
return _mapPlatform!.getPlatformName();
|
|
|
}
|
|
|
|
|
|
static void receiveLocationStream({required MapLocationListener listener}) {
|
|
|
- assert(_mapPlatform != null, "请先初始化地图");
|
|
|
+ assert(_mapPlatform != null, "请先调用 FlutterMap.init()");
|
|
|
_mapPlatform!.receiveLocationStream(listener: listener);
|
|
|
}
|
|
|
|
|
|
static Future<void> startLocation() {
|
|
|
- assert(_mapPlatform != null, "请先初始化地图");
|
|
|
+ assert(_mapPlatform != null, "请先调用 FlutterMap.init()");
|
|
|
return _mapPlatform!.startLocation();
|
|
|
}
|
|
|
|
|
|
static Future<List<LatLng>?> queryProcessedTrace(
|
|
|
{required int lineID, required List<TraceLocation> locations}) {
|
|
|
- assert(_mapPlatform != null, "请先初始化地图");
|
|
|
+ assert(_mapPlatform != null, "请先调用 FlutterMap.init()");
|
|
|
return _mapPlatform!
|
|
|
.queryProcessedTrace(lineID: lineID, locations: locations);
|
|
|
}
|