AMapPolygon.m 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // AMapPolygon.m
  3. // amap_flutter_map
  4. //
  5. // Created by lly on 2020/11/12.
  6. //
  7. #import "AMapPolygon.h"
  8. #import "AMapConvertUtil.h"
  9. #import "MAPolygon+Flutter.h"
  10. @interface AMapPolygon ()
  11. @property (nonatomic, strong,readwrite) MAPolygon *polygon;
  12. @end
  13. @implementation AMapPolygon
  14. - (instancetype)init {
  15. self = [super init];
  16. if (self) {
  17. _visible = YES;
  18. }
  19. return self;
  20. }
  21. - (void)postHookWith:(NSDictionary *)dict {
  22. NSArray *points = dict[@"points"];
  23. NSAssert(points.count > 0, @"polygon传入的经纬度点有误!");
  24. //如果经纬度点已经有值,需要手动释放内存
  25. if (_coords != NULL) {
  26. free(_coords);
  27. _coords = NULL;
  28. }
  29. _coordCount = points.count;
  30. _coords = (CLLocationCoordinate2D*)malloc(_coordCount * sizeof(CLLocationCoordinate2D));
  31. for (NSUInteger index = 0; index < _coordCount; index ++) {
  32. NSArray *point = points[index];
  33. _coords[index] = [AMapConvertUtil coordinateFromArray:point];
  34. }
  35. }
  36. - (void)dealloc {
  37. if (_coords != NULL) {
  38. free(_coords);
  39. _coords = NULL;
  40. }
  41. }
  42. - (MAPolygon *)polygon {
  43. if (_polygon == nil) {
  44. _polygon = [[MAPolygon alloc] initWithPolygonId:self.id_];
  45. [_polygon setPolygonWithCoordinates:_coords count:_coordCount];
  46. }
  47. return _polygon;
  48. }
  49. //更新polyline
  50. - (void)updatePolygon:(AMapPolygon *)polygon {
  51. NSAssert((polygon != nil && [self.id_ isEqualToString:polygon.id_]), @"更新AMapPolygon数据异常");
  52. if ([self checkCoordsEqualWithPolyline:polygon] == NO) {//polyline更新了经纬度坐标
  53. if (_coords != NULL) {
  54. free(_coords);
  55. _coords = NULL;
  56. }
  57. _coordCount = polygon->_coordCount;
  58. _coords = (CLLocationCoordinate2D*)malloc(_coordCount * sizeof(CLLocationCoordinate2D));
  59. for (NSUInteger index = 0; index < _coordCount; index ++) {
  60. _coords[index] = polygon->_coords[index];
  61. }
  62. }
  63. self.strokeWidth = polygon.strokeWidth;
  64. self.strokeColor = polygon.strokeColor;
  65. self.fillColor = polygon.fillColor;
  66. self.visible = polygon.visible;
  67. self.joinType = polygon.joinType;
  68. if (_polygon) {
  69. [_polygon setPolygonWithCoordinates:_coords count:_coordCount];
  70. }
  71. }
  72. - (BOOL)checkCoordsEqualWithPolyline:(AMapPolygon *)newPolygon {
  73. if (_coordCount != newPolygon->_coordCount) {//数量不同,则直接更新
  74. return NO;
  75. }
  76. for (NSUInteger index = 0; index < _coordCount; index++) {
  77. if ([AMapConvertUtil isEqualWith:_coords[index] to:newPolygon->_coords[index]] == NO) {
  78. return NO;
  79. }
  80. }
  81. return YES;
  82. }
  83. @end