AMapPolylineController.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // AMapPolylineController.m
  3. // amap_flutter_map
  4. //
  5. // Created by lly on 2020/11/6.
  6. //
  7. #import "AMapPolylineController.h"
  8. #import "AMapPolyline.h"
  9. #import "AMapJsonUtils.h"
  10. #import "AMapMarker.h"
  11. #import "MAPolyline+Flutter.h"
  12. #import "MAPolylineRenderer+Flutter.h"
  13. #import "AMapConvertUtil.h"
  14. #import "FlutterMethodChannel+MethodCallDispatch.h"
  15. @interface AMapPolylineController ()
  16. @property (nonatomic,strong) NSMutableDictionary<NSString*,AMapPolyline*> *polylineDict;
  17. @property (nonatomic,strong) FlutterMethodChannel *methodChannel;
  18. @property (nonatomic,strong) NSObject<FlutterPluginRegistrar> *registrar;
  19. @property (nonatomic,strong) MAMapView *mapView;
  20. @end
  21. @implementation AMapPolylineController
  22. - (instancetype)init:(FlutterMethodChannel*)methodChannel
  23. mapView:(MAMapView*)mapView
  24. registrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  25. self = [super init];
  26. if (self) {
  27. _methodChannel = methodChannel;
  28. _mapView = mapView;
  29. _polylineDict = [NSMutableDictionary dictionaryWithCapacity:1];
  30. _registrar = registrar;
  31. __weak typeof(self) weakSelf = self;
  32. [_methodChannel addMethodName:@"polylines#update" withHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult _Nonnull result) {
  33. id polylinesToAdd = call.arguments[@"polylinesToAdd"];
  34. if ([polylinesToAdd isKindOfClass:[NSArray class]]) {
  35. [weakSelf addPolylines:polylinesToAdd];
  36. }
  37. id polylinesToChange = call.arguments[@"polylinesToChange"];
  38. if ([polylinesToChange isKindOfClass:[NSArray class]]) {
  39. [weakSelf changePolylines:polylinesToChange];
  40. }
  41. id polylineIdsToRemove = call.arguments[@"polylineIdsToRemove"];
  42. if ([polylineIdsToRemove isKindOfClass:[NSArray class]]) {
  43. [weakSelf removePolylineIds:polylineIdsToRemove];
  44. }
  45. result(nil);
  46. }];
  47. }
  48. return self;
  49. }
  50. - (nullable AMapPolyline *)polylineForId:(NSString *)polylineId {
  51. return _polylineDict[polylineId];
  52. }
  53. - (void)addPolylines:(NSArray*)polylinesToAdd {
  54. for (NSDictionary* polyline in polylinesToAdd) {
  55. AMapPolyline *polylineModel = [AMapJsonUtils modelFromDict:polyline modelClass:[AMapPolyline class]];
  56. if (polylineModel.customTexture) {
  57. polylineModel.strokeImage = [AMapConvertUtil imageFromRegistrar:self.registrar iconData:polylineModel.customTexture];
  58. }
  59. // 先加入到字段中,避免后续的地图回到里,取不到对应的overlay数据
  60. if (polylineModel.id_) {
  61. _polylineDict[polylineModel.id_] = polylineModel;
  62. }
  63. [self.mapView addOverlay:polylineModel.polyline];
  64. }
  65. }
  66. - (void)changePolylines:(NSArray*)polylinesToChange {
  67. for (NSDictionary* polylineToChange in polylinesToChange) {
  68. AMapPolyline *polyline = [AMapJsonUtils modelFromDict:polylineToChange modelClass:[AMapPolyline class]];
  69. AMapPolyline *currentPolyline = _polylineDict[polyline.id_];
  70. NSAssert(currentPolyline != nil, @"需要修改的Polyline不存在");
  71. // 如果图片纹理变了,则存储和解析新的图标
  72. if ([AMapConvertUtil checkIconDescriptionChangedFrom:currentPolyline.customTexture to:polyline.customTexture]) {
  73. currentPolyline.strokeImage = [AMapConvertUtil imageFromRegistrar:self.registrar iconData:polyline.customTexture];
  74. currentPolyline.customTexture = polyline.customTexture;
  75. }
  76. //更新除了图标之外的其它信息
  77. [currentPolyline updatePolyline:polyline];
  78. MAOverlayRenderer *render = [self.mapView rendererForOverlay:currentPolyline.polyline];
  79. if (render && [render isKindOfClass:[MAPolylineRenderer class]]) { // render没有复用,只要添加过,就一定可以获取到
  80. [(MAPolylineRenderer *)render updateRenderWithPolyline:currentPolyline];
  81. }
  82. }
  83. }
  84. - (void)removePolylineIds:(NSArray*)polylineIdsToRemove {
  85. for (NSString* polylineId in polylineIdsToRemove) {
  86. if (!polylineId) {
  87. continue;
  88. }
  89. AMapPolyline* polyline = _polylineDict[polylineId];
  90. if (!polyline) {
  91. continue;
  92. }
  93. [self.mapView removeOverlay:polyline.polyline];
  94. [_polylineDict removeObjectForKey:polylineId];
  95. }
  96. }
  97. //MARK: Marker的回调
  98. - (BOOL)onPolylineTap:(NSString*)polylineId {
  99. if (!polylineId) {
  100. return NO;
  101. }
  102. AMapPolyline* polyline = _polylineDict[polylineId];
  103. if (!polyline) {
  104. return NO;
  105. }
  106. [_methodChannel invokeMethod:@"polyline#onTap" arguments:@{@"polylineId" : polylineId}];
  107. return YES;
  108. }
  109. @end