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