amap_location.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'package:flutter/services.dart';
  5. import 'package:flutter/material.dart';
  6. import 'amap_marker.dart';
  7. import 'location_option.dart';
  8. import 'location_result_entity.dart';
  9. // IOS:
  10. // add info.plist GaoDeAppKey
  11. class AmapLocation {
  12. static const MethodChannel _channel = const MethodChannel('amap_location');
  13. static final AmapLocation _instance = AmapLocation();
  14. static const String viewType = "com.i2edu.mapView";
  15. static AmapLocation get instance => _instance;
  16. StreamController _locationStreamController;
  17. StreamController _guideStreamController;
  18. Stream<LocationResultEntity> get locationStream => _locationStreamController.stream;
  19. Stream<Map> get guideStream => _guideStreamController.stream;
  20. AmapLocation() {
  21. _channel.setMethodCallHandler(platformCallHandler);
  22. _locationStreamController = StreamController<LocationResultEntity>.broadcast();
  23. _guideStreamController = StreamController<Map>.broadcast();
  24. }
  25. static Future<String> get platformVersion async {
  26. final String version = await _channel.invokeMethod('getPlatformVersion');
  27. return version;
  28. }
  29. static Future<void> registerView() async {
  30. return await _channel.invokeMethod('registerView');
  31. }
  32. Widget buildMapView(VoidCallback onPlatformViewCreated) =>
  33. Platform.isAndroid
  34. ? AndroidView(
  35. viewType: viewType,
  36. creationParams: {},
  37. creationParamsCodec: const StandardMessageCodec(),
  38. onPlatformViewCreated: (id) => onPlatformViewCreated(),
  39. ) : UiKitView(
  40. viewType: viewType,
  41. creationParams: {},
  42. creationParamsCodec: const StandardMessageCodec(),
  43. onPlatformViewCreated: (id) => onPlatformViewCreated(),
  44. );
  45. Future<void> setMapMarkers(List<AmapMarker> markers) async {
  46. return await _channel.invokeMethod("setMarkers", {"markers": markers.map((f) => f.toMap()).toList()});
  47. }
  48. Future<void> onCreateMapView() async {
  49. return await _channel.invokeMethod('onCreate');
  50. }
  51. Future<void> onPauseMapView() async {
  52. return await _channel.invokeMethod('onPause');
  53. }
  54. Future<void> onResumeMapView() async {
  55. return await _channel.invokeMethod('onResume');
  56. }
  57. Future<void> startLocation({LocationOption options}) async {
  58. if (_locationStreamController == null) {
  59. _locationStreamController = StreamController<LocationResultEntity>.broadcast();
  60. }
  61. return await _channel.invokeMethod(
  62. 'startLocation', options?.toMap() ?? LocationOption().toMap());
  63. }
  64. Future<void> disposed() async {
  65. if (!_locationStreamController.isClosed) {
  66. await _locationStreamController.close();
  67. }
  68. _locationStreamController = null;
  69. return await _channel.invokeMethod("closeLocation");
  70. }
  71. Future<void> disposedMapView() async {
  72. if (!_guideStreamController.isClosed) {
  73. await _guideStreamController.close();
  74. }
  75. _guideStreamController = null;
  76. return;
  77. }
  78. Future<void> platformCallHandler(MethodCall call) async {
  79. try {
  80. if (call.method == "location") {
  81. LocationResultEntity entity =
  82. LocationResultEntity().fromJson(jsonDecode(call.arguments));
  83. _locationStreamController.add(entity);
  84. } else if (call.method == "guide") {
  85. _guideStreamController.add(call.arguments);
  86. }
  87. } catch (ex) {
  88. print('Unexpected error: $ex');
  89. }
  90. return null;
  91. }
  92. }