AMapPolygon.m
2.61 KB
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
//
// AMapPolygon.m
// amap_flutter_map
//
// Created by lly on 2020/11/12.
//
#import "AMapPolygon.h"
#import "AMapConvertUtil.h"
#import "MAPolygon+Flutter.h"
@interface AMapPolygon ()
@property (nonatomic, strong,readwrite) MAPolygon *polygon;
@end
@implementation AMapPolygon
- (instancetype)init {
self = [super init];
if (self) {
_visible = YES;
}
return self;
}
- (void)postHookWith:(NSDictionary *)dict {
NSArray *points = dict[@"points"];
NSAssert(points.count > 0, @"polygon传入的经纬度点有误!");
//如果经纬度点已经有值,需要手动释放内存
if (_coords != NULL) {
free(_coords);
_coords = NULL;
}
_coordCount = points.count;
_coords = (CLLocationCoordinate2D*)malloc(_coordCount * sizeof(CLLocationCoordinate2D));
for (NSUInteger index = 0; index < _coordCount; index ++) {
NSArray *point = points[index];
_coords[index] = [AMapConvertUtil coordinateFromArray:point];
}
}
- (void)dealloc {
if (_coords != NULL) {
free(_coords);
_coords = NULL;
}
}
- (MAPolygon *)polygon {
if (_polygon == nil) {
_polygon = [[MAPolygon alloc] initWithPolygonId:self.id_];
[_polygon setPolygonWithCoordinates:_coords count:_coordCount];
}
return _polygon;
}
//更新polyline
- (void)updatePolygon:(AMapPolygon *)polygon {
NSAssert((polygon != nil && [self.id_ isEqualToString:polygon.id_]), @"更新AMapPolygon数据异常");
if ([self checkCoordsEqualWithPolyline:polygon] == NO) {//polyline更新了经纬度坐标
if (_coords != NULL) {
free(_coords);
_coords = NULL;
}
_coordCount = polygon->_coordCount;
_coords = (CLLocationCoordinate2D*)malloc(_coordCount * sizeof(CLLocationCoordinate2D));
for (NSUInteger index = 0; index < _coordCount; index ++) {
_coords[index] = polygon->_coords[index];
}
}
self.strokeWidth = polygon.strokeWidth;
self.strokeColor = polygon.strokeColor;
self.fillColor = polygon.fillColor;
self.visible = polygon.visible;
self.joinType = polygon.joinType;
if (_polygon) {
[_polygon setPolygonWithCoordinates:_coords count:_coordCount];
}
}
- (BOOL)checkCoordsEqualWithPolyline:(AMapPolygon *)newPolygon {
if (_coordCount != newPolygon->_coordCount) {//数量不同,则直接更新
return NO;
}
for (NSUInteger index = 0; index < _coordCount; index++) {
if ([AMapConvertUtil isEqualWith:_coords[index] to:newPolygon->_coords[index]] == NO) {
return NO;
}
}
return YES;
}
@end