|
@@ -1,23 +1,33 @@
|
|
|
import 'dart:async';
|
|
import 'dart:async';
|
|
|
import 'dart:convert';
|
|
import 'dart:convert';
|
|
|
|
|
+import 'dart:io';
|
|
|
|
|
|
|
|
-import 'package:amap_location/location_option.dart';
|
|
|
|
|
-import 'package:amap_location/location_result_entity.dart';
|
|
|
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
|
+import 'package:flutter/material.dart';
|
|
|
|
|
+
|
|
|
|
|
+import 'amap_marker.dart';
|
|
|
|
|
+import 'location_option.dart';
|
|
|
|
|
+import 'location_result_entity.dart';
|
|
|
|
|
|
|
|
// IOS:
|
|
// IOS:
|
|
|
// add info.plist GaoDeAppKey
|
|
// add info.plist GaoDeAppKey
|
|
|
class AmapLocation {
|
|
class AmapLocation {
|
|
|
static const MethodChannel _channel = const MethodChannel('amap_location');
|
|
static const MethodChannel _channel = const MethodChannel('amap_location');
|
|
|
static final AmapLocation _instance = AmapLocation();
|
|
static final AmapLocation _instance = AmapLocation();
|
|
|
|
|
+ static const String viewType = "com.i2edu.mapView";
|
|
|
|
|
+
|
|
|
static AmapLocation get instance => _instance;
|
|
static AmapLocation get instance => _instance;
|
|
|
|
|
|
|
|
- StreamController _streamController;
|
|
|
|
|
- Stream<LocationResultEntity> get locationStream => _streamController.stream;
|
|
|
|
|
|
|
+ StreamController _locationStreamController;
|
|
|
|
|
+ StreamController _guideStreamController;
|
|
|
|
|
+
|
|
|
|
|
+ Stream<LocationResultEntity> get locationStream => _locationStreamController.stream;
|
|
|
|
|
+ Stream<Map> get guideStream => _guideStreamController.stream;
|
|
|
|
|
|
|
|
- AmapLocation(){
|
|
|
|
|
|
|
+ AmapLocation() {
|
|
|
_channel.setMethodCallHandler(platformCallHandler);
|
|
_channel.setMethodCallHandler(platformCallHandler);
|
|
|
- _streamController = StreamController<LocationResultEntity>.broadcast();
|
|
|
|
|
|
|
+ _locationStreamController = StreamController<LocationResultEntity>.broadcast();
|
|
|
|
|
+ _guideStreamController = StreamController<Map>.broadcast();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
static Future<String> get platformVersion async {
|
|
static Future<String> get platformVersion async {
|
|
@@ -25,33 +35,76 @@ class AmapLocation {
|
|
|
return version;
|
|
return version;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ static Future<void> registerView() async {
|
|
|
|
|
+ return await _channel.invokeMethod('registerView');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Widget buildMapView(VoidCallback onPlatformViewCreated) =>
|
|
|
|
|
+ Platform.isAndroid
|
|
|
|
|
+ ? AndroidView(
|
|
|
|
|
+ viewType: viewType,
|
|
|
|
|
+ creationParams: {},
|
|
|
|
|
+ creationParamsCodec: const StandardMessageCodec(),
|
|
|
|
|
+ onPlatformViewCreated: (id) => onPlatformViewCreated(),
|
|
|
|
|
+ ) : UiKitView(
|
|
|
|
|
+ viewType: viewType,
|
|
|
|
|
+ creationParams: {},
|
|
|
|
|
+ creationParamsCodec: const StandardMessageCodec(),
|
|
|
|
|
+ onPlatformViewCreated: (id) => onPlatformViewCreated(),
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ Future<void> setMapMarkers(List<AmapMarker> markers) async {
|
|
|
|
|
+ return await _channel.invokeMethod("setMarkers", {"markers": markers.map((f) => f.toMap()).toList()});
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Future<void> onCreateMapView() async {
|
|
|
|
|
+ return await _channel.invokeMethod('onCreate');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Future<void> onPauseMapView() async {
|
|
|
|
|
+ return await _channel.invokeMethod('onPause');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Future<void> onResumeMapView() async {
|
|
|
|
|
+ return await _channel.invokeMethod('onResume');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
Future<void> startLocation({LocationOption options}) async {
|
|
Future<void> startLocation({LocationOption options}) async {
|
|
|
- if (_streamController == null) {
|
|
|
|
|
- _streamController = StreamController<LocationResultEntity>.broadcast();
|
|
|
|
|
|
|
+ if (_locationStreamController == null) {
|
|
|
|
|
+ _locationStreamController = StreamController<LocationResultEntity>.broadcast();
|
|
|
}
|
|
}
|
|
|
return await _channel.invokeMethod(
|
|
return await _channel.invokeMethod(
|
|
|
'startLocation', options?.toMap() ?? LocationOption().toMap());
|
|
'startLocation', options?.toMap() ?? LocationOption().toMap());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Future<void> disposed() async {
|
|
Future<void> disposed() async {
|
|
|
- if (!_streamController.isClosed) {
|
|
|
|
|
- await _streamController.close();
|
|
|
|
|
|
|
+ if (!_locationStreamController.isClosed) {
|
|
|
|
|
+ await _locationStreamController.close();
|
|
|
}
|
|
}
|
|
|
- _streamController = null;
|
|
|
|
|
|
|
+ _locationStreamController = null;
|
|
|
return await _channel.invokeMethod("closeLocation");
|
|
return await _channel.invokeMethod("closeLocation");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ Future<void> disposedMapView() async {
|
|
|
|
|
+ if (!_guideStreamController.isClosed) {
|
|
|
|
|
+ await _guideStreamController.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ _guideStreamController = null;
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
Future<void> platformCallHandler(MethodCall call) async {
|
|
Future<void> platformCallHandler(MethodCall call) async {
|
|
|
try {
|
|
try {
|
|
|
if (call.method == "location") {
|
|
if (call.method == "location") {
|
|
|
LocationResultEntity entity =
|
|
LocationResultEntity entity =
|
|
|
- LocationResultEntity().fromJson(jsonDecode(call.arguments));
|
|
|
|
|
- _streamController.add(entity);
|
|
|
|
|
|
|
+ LocationResultEntity().fromJson(jsonDecode(call.arguments));
|
|
|
|
|
+ _locationStreamController.add(entity);
|
|
|
|
|
+ } else if (call.method == "guide") {
|
|
|
|
|
+ _guideStreamController.add(call.arguments);
|
|
|
}
|
|
}
|
|
|
} catch (ex) {
|
|
} catch (ex) {
|
|
|
print('Unexpected error: $ex');
|
|
print('Unexpected error: $ex');
|
|
|
}
|
|
}
|
|
|
return null;
|
|
return null;
|
|
|
-
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|