AMapPolyline.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // AMapPolyline.m
  3. // amap_flutter_map
  4. //
  5. // Created by lly on 2020/11/6.
  6. //
  7. #import "AMapPolyline.h"
  8. #import "HSTCusPolyline.h"
  9. #import "AMapConvertUtil.h"
  10. #import "MAPolyline+Flutter.h"
  11. @interface AMapPolyline ()
  12. @property (nonatomic, strong, readwrite) HSTCusPolyline *polyline;
  13. @end
  14. @implementation AMapPolyline
  15. - (instancetype)init {
  16. self = [super init];
  17. if (self) {
  18. _alpha = 1.0;
  19. _visible = YES;
  20. }
  21. return self;
  22. }
  23. - (void)postHookWith:(NSDictionary *)dict {
  24. NSArray *points = dict[@"points"];
  25. NSAssert(points.count > 0, @"polyline传入的经纬度点有误!");
  26. //如果经纬度点已经有值,需要手动释放内存
  27. if (_coords != NULL) {
  28. free(_coords);
  29. _coords = NULL;
  30. }
  31. _coordCount = points.count;
  32. _coords = (CLLocationCoordinate2D*)malloc(_coordCount * sizeof(CLLocationCoordinate2D));
  33. for (NSUInteger index = 0; index < _coordCount; index ++) {
  34. NSArray *point = points[index];
  35. _coords[index] = [AMapConvertUtil coordinateFromArray:point];
  36. }
  37. _colorList = dict[@"colorList"];
  38. }
  39. - (HSTCusPolyline *)polyline {
  40. if (_polyline == nil) {
  41. _polyline = [[HSTCusPolyline alloc] initWithPolylineId:self.id_];
  42. _polyline.dashLineType = _dashLineType;
  43. _polyline.capType = _capType;
  44. _polyline.joinType = _joinType;
  45. _polyline.lineWidth = _width;
  46. if(_colorList){
  47. NSInteger count =_colorList.count;
  48. NSMutableArray *mutArr = [NSMutableArray arrayWithCapacity:count];
  49. NSMutableArray *mutArrIndex = [NSMutableArray arrayWithCapacity:count];
  50. for(int i = 0; i < count;i++){
  51. NSNumber* item =_colorList[i];
  52. UIColor *color = [AMapConvertUtil colorFromNumber:item];
  53. [mutArr addObject:color];
  54. [mutArrIndex addObject:[NSNumber numberWithInt:i]];
  55. }
  56. _polyline.colorsArr = mutArr;
  57. [_polyline setPolylineWithCoordinates:_coords count:_coordCount drawStyleIndexes:@[@1, @(_coordCount-1)]];
  58. }else{
  59. [_polyline setPolylineWithCoordinates:_coords count:_coordCount];
  60. }
  61. }
  62. return _polyline;
  63. }
  64. - (void)dealloc {
  65. if (_coords != NULL) {
  66. free(_coords);
  67. _coords = NULL;
  68. }
  69. }
  70. //更新polyline
  71. - (void)updatePolyline:(AMapPolyline *)polyline {
  72. NSAssert((polyline != nil && [self.id_ isEqualToString:polyline.id_]), @"更新Polyline数据异常");
  73. if ([self checkCoordsEqualWithPolyline:polyline] == NO) {//polyline更新了经纬度坐标
  74. if (_coords != NULL) {
  75. free(_coords);
  76. _coords = NULL;
  77. }
  78. _coordCount = polyline->_coordCount;
  79. _coords = (CLLocationCoordinate2D*)malloc(_coordCount * sizeof(CLLocationCoordinate2D));
  80. for (NSUInteger index = 0; index < _coordCount; index ++) {
  81. _coords[index] = polyline->_coords[index];
  82. }
  83. }
  84. self.width = polyline.width;
  85. self.color = polyline.color;
  86. self.visible = polyline.visible;
  87. self.alpha = polyline.alpha;
  88. NSAssert(self.geodesic == polyline.geodesic, @"是否为大地曲线的变量,不允许动态修改");
  89. self.dashLineType = polyline.dashLineType;
  90. self.joinType = polyline.joinType;
  91. self.capType = polyline.capType;
  92. if (_polyline) {
  93. [_polyline setPolylineWithCoordinates:_coords count:_coordCount];
  94. }
  95. }
  96. - (BOOL)checkCoordsEqualWithPolyline:(AMapPolyline *)newPolyline {
  97. if (_coordCount != newPolyline->_coordCount) {//数量不同,则直接更新
  98. return NO;
  99. }
  100. for (NSUInteger index = 0; index < _coordCount; index++) {
  101. if ([AMapConvertUtil isEqualWith:_coords[index] to:newPolyline->_coords[index]] == NO) {
  102. return NO;
  103. }
  104. }
  105. return YES;
  106. }
  107. @end