AMapPolygonController.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // AMapPolygonController.m
  3. // amap_flutter_map
  4. //
  5. // Created by lly on 2020/11/12.
  6. //
  7. #import "AMapPolygonController.h"
  8. #import "AMapPolygon.h"
  9. #import "AMapJsonUtils.h"
  10. #import "MAPolygon+Flutter.h"
  11. #import "MAPolygonRenderer+Flutter.h"
  12. #import "FlutterMethodChannel+MethodCallDispatch.h"
  13. @interface AMapPolygonController ()
  14. @property (nonatomic,strong) NSMutableDictionary<NSString*,AMapPolygon*> *polygonDict;
  15. @property (nonatomic,strong) FlutterMethodChannel *methodChannel;
  16. @property (nonatomic,strong) NSObject<FlutterPluginRegistrar> *registrar;
  17. @property (nonatomic,strong) MAMapView *mapView;
  18. @end
  19. @implementation AMapPolygonController
  20. - (instancetype)init:(FlutterMethodChannel*)methodChannel
  21. mapView:(MAMapView*)mapView
  22. registrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  23. self = [super init];
  24. if (self) {
  25. _methodChannel = methodChannel;
  26. _mapView = mapView;
  27. _polygonDict = [NSMutableDictionary dictionaryWithCapacity:1];
  28. _registrar = registrar;
  29. __weak typeof(self) weakSelf = self;
  30. [_methodChannel addMethodName:@"polygons#update" withHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult _Nonnull result) {
  31. id polygonsToAdd = call.arguments[@"polygonsToAdd"];
  32. if ([polygonsToAdd isKindOfClass:[NSArray class]]) {
  33. [weakSelf addPolygons:polygonsToAdd];
  34. }
  35. id polygonsToChange = call.arguments[@"polygonsToChange"];
  36. if ([polygonsToChange isKindOfClass:[NSArray class]]) {
  37. [weakSelf changePolygons:polygonsToChange];
  38. }
  39. id polygonIdsToRemove = call.arguments[@"polygonIdsToRemove"];
  40. if ([polygonIdsToRemove isKindOfClass:[NSArray class]]) {
  41. [weakSelf removePolygonIds:polygonIdsToRemove];
  42. }
  43. result(nil);
  44. }];
  45. }
  46. return self;
  47. }
  48. - (nullable AMapPolygon *)polygonForId:(NSString *)polygonId {
  49. return _polygonDict[polygonId];
  50. }
  51. - (void)addPolygons:(NSArray*)polygonsToAdd {
  52. for (NSDictionary* polygonDict in polygonsToAdd) {
  53. AMapPolygon *polygon = [AMapJsonUtils modelFromDict:polygonDict modelClass:[AMapPolygon class]];
  54. // 先加入到字段中,避免后续的地图回到里,取不到对应的overlay数据
  55. if (polygon.id_) {
  56. _polygonDict[polygon.id_] = polygon;
  57. }
  58. [self.mapView addOverlay:polygon.polygon];
  59. }
  60. }
  61. - (void)changePolygons:(NSArray*)polygonsToChange {
  62. for (NSDictionary* polygonDict in polygonsToChange) {
  63. AMapPolygon *polygon = [AMapJsonUtils modelFromDict:polygonDict modelClass:[AMapPolygon class]];
  64. AMapPolygon *currentPolygon = _polygonDict[polygon.id_];
  65. NSAssert(currentPolygon != nil, @"需要修改的Polygon不存在");
  66. [currentPolygon updatePolygon:polygon];
  67. MAOverlayRenderer *render = [self.mapView rendererForOverlay:currentPolygon.polygon];
  68. if (render && [render isKindOfClass:[MAPolygonRenderer class]]) { // render没有复用,只要添加过,就一定可以获取到
  69. [(MAPolygonRenderer *)render updateRenderWithPolygon:currentPolygon];
  70. }
  71. }
  72. }
  73. - (void)removePolygonIds:(NSArray*)polygonIdsToRemove {
  74. for (NSString* polygonId in polygonIdsToRemove) {
  75. if (!polygonId) {
  76. continue;
  77. }
  78. AMapPolygon* polygon = _polygonDict[polygonId];
  79. if (!polygon) {
  80. continue;
  81. }
  82. [self.mapView removeOverlay:polygon.polygon];
  83. [_polygonDict removeObjectForKey:polygonId];
  84. }
  85. }
  86. @end