amap_view_controller.dart 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import 'dart:async';
  2. import 'package:flutter/services.dart';
  3. import 'amap_marker.dart';
  4. class AmapViewController {
  5. MethodChannel _methodChannel;
  6. StreamController _guideStreamController;
  7. Stream<Map> get guideStream => _guideStreamController.stream;
  8. void init(int id) {
  9. _methodChannel = MethodChannel("com.i2edu.mapView/map_view_$id");
  10. _methodChannel.setMethodCallHandler(platformCallHandler);
  11. _guideStreamController = StreamController<Map>.broadcast();
  12. }
  13. Future<void> platformCallHandler(MethodCall call) async {
  14. try {
  15. if (call.method == "guide") {
  16. _guideStreamController.add(call.arguments);
  17. }
  18. } catch (ex) {
  19. print('Unexpected error: $ex');
  20. }
  21. return null;
  22. }
  23. Future<void> setMapMarkers(List<AmapMarker> markers) async {
  24. assert(_methodChannel != null);
  25. return await _methodChannel.invokeMethod(
  26. "setMarkers", {"markers": markers.map((f) => f.toMap()).toList()});
  27. }
  28. Future<void> onCreateMapView() async {
  29. assert(_methodChannel != null);
  30. return await _methodChannel.invokeMethod('onCreate');
  31. }
  32. Future<void> onPauseMapView() async {
  33. assert(_methodChannel != null);
  34. return await _methodChannel.invokeMethod('onPause');
  35. }
  36. Future<void> onResumeMapView() async {
  37. assert(_methodChannel != null);
  38. return await _methodChannel.invokeMethod('onResume');
  39. }
  40. void dispose() {
  41. if (!_guideStreamController.isClosed) {
  42. _guideStreamController.close();
  43. }
  44. }
  45. }