AMapConvertUtil.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // AMapConvertUtil.m
  3. // amap_flutter_map
  4. //
  5. // Created by lly on 2020/10/30.
  6. //
  7. #import "AMapConvertUtil.h"
  8. @implementation AMapConvertUtil
  9. /// 经纬度坐标转字符串
  10. /// @param coordinate 经纬度坐标
  11. + (NSString *)stringFromCoordinate:(CLLocationCoordinate2D)coordinate {
  12. return [NSString stringWithFormat:@"{%.6f,%.6f}", coordinate.longitude, coordinate.latitude];
  13. }
  14. + (UIColor*)colorFromNumber:(NSNumber*)numberColor {
  15. if (numberColor == nil || [numberColor isKindOfClass:[NSNumber class]] == NO) {
  16. return nil;
  17. }
  18. unsigned long value = [numberColor unsignedLongValue];
  19. return [UIColor colorWithRed:((float)((value & 0xFF0000) >> 16)) / 255.0
  20. green:((float)((value & 0xFF00) >> 8)) / 255.0
  21. blue:((float)(value & 0xFF)) / 255.0
  22. alpha:((float)((value & 0xFF000000) >> 24)) / 255.0];
  23. }
  24. + (CGPoint)pointFromArray:(NSArray*)data {
  25. NSAssert((data != nil && [data isKindOfClass:[NSArray class]] && data.count == 2), @"数组类型转point格式错误");
  26. return CGPointMake([data[0] doubleValue],
  27. [data[1] doubleValue]);
  28. }
  29. /// 从数据中解析经纬度
  30. /// @param array 经纬度数组对(默认第一个当做维度,第二个当做经度)
  31. + (CLLocationCoordinate2D)coordinateFromArray:(NSArray *)array {
  32. CLLocationCoordinate2D location = kCLLocationCoordinate2DInvalid;
  33. if (array.count == 2) {
  34. double latitude = [array[0] doubleValue];
  35. double longitude = [array[1] doubleValue];
  36. if ([self checkValidLatitude:latitude longitude:longitude]) {
  37. location = CLLocationCoordinate2DMake(latitude, longitude);
  38. } else if ([self checkValidLatitude:longitude longitude:latitude]) {//交换二者
  39. location = CLLocationCoordinate2DMake(longitude, latitude);
  40. } else {
  41. NSLog(@"经纬度参数异常,解析为无效经纬度");
  42. }
  43. } else {
  44. NSLog(@"经纬度参数异常,解析为无效经纬度");
  45. }
  46. return location;
  47. }
  48. + (NSArray *)jsonFromCoordinate:(CLLocationCoordinate2D )coord {
  49. if (CLLocationCoordinate2DIsValid(coord)) {
  50. return @[@(coord.latitude),@(coord.longitude)];
  51. } else {
  52. NSLog(@"经纬度无效,返回为空");
  53. return @[];
  54. }
  55. }
  56. /// 检测经纬度是否有效
  57. /// @param latitude 维度
  58. /// @param longitude 经度
  59. + (BOOL)checkValidLatitude:(double)latitude longitude:(double)longitude {
  60. if (latitude > 90 || latitude < -90) {
  61. return false;
  62. }
  63. if (longitude > 180 || longitude < -180) {
  64. return false;
  65. }
  66. return true;
  67. }
  68. + (NSArray<NSNumber *> *)jsonArrayFromCoordinate:(CLLocationCoordinate2D)coordinate {
  69. if (CLLocationCoordinate2DIsValid(coordinate)) {
  70. return @[ @(coordinate.latitude), @(coordinate.longitude) ];
  71. } else {
  72. return @[];
  73. }
  74. }
  75. + (UIImage*)scaleImage:(UIImage*)image param:(NSNumber*)scaleParam {
  76. double scale = 1.0;
  77. if ([scaleParam isKindOfClass:[NSNumber class]]) {
  78. scale = scaleParam.doubleValue;
  79. }
  80. if (fabs(scale - 1) > 1e-3) {
  81. return [UIImage imageWithCGImage:[image CGImage]
  82. scale:(image.scale * scale)
  83. orientation:(image.imageOrientation)];
  84. }
  85. return image;
  86. }
  87. + (UIImage*)imageFromRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar iconData:(NSArray*)iconData {
  88. UIImage* image;
  89. if ([iconData.firstObject isEqualToString:@"defaultMarker"]) {
  90. image = [UIImage imageNamed:[registrar lookupKeyForAsset:@"packages/amap_flutter_map/res/marker_default.png"]];//默认的图片资源
  91. CGFloat screenScale = [[UIScreen mainScreen] scale];
  92. image = [self scaleImage:image param:[NSNumber numberWithFloat:screenScale]];
  93. //添加默认图片
  94. } else if ([iconData.firstObject isEqualToString:@"fromAsset"]) {
  95. if (iconData.count == 2) {
  96. image = [UIImage imageNamed:[registrar lookupKeyForAsset:iconData[1]]];
  97. CGFloat screenScale = [[UIScreen mainScreen] scale];
  98. image = [self scaleImage:image param:[NSNumber numberWithFloat:screenScale]];
  99. }
  100. } else if ([iconData.firstObject isEqualToString:@"fromAssetImage"]) {
  101. if (iconData.count == 3) {
  102. image = [UIImage imageNamed:[registrar lookupKeyForAsset:iconData[1]]];
  103. NSNumber* scaleParam = iconData[2];
  104. image = [self scaleImage:image param:scaleParam];
  105. } else {
  106. NSString* error =
  107. [NSString stringWithFormat:@"'fromAssetImage' should have exactly 3 arguments. Got: %lu",
  108. (unsigned long)iconData.count];
  109. NSException* exception = [NSException exceptionWithName:@"InvalidBitmapDescriptor"
  110. reason:error
  111. userInfo:nil];
  112. @throw exception;
  113. }
  114. } else if ([iconData[0] isEqualToString:@"fromBytes"]) {
  115. if (iconData.count == 2) {
  116. @try {
  117. FlutterStandardTypedData* byteData = iconData[1];
  118. CGFloat screenScale = [[UIScreen mainScreen] scale];
  119. image = [UIImage imageWithData:[byteData data] scale:screenScale];
  120. } @catch (NSException* exception) {
  121. @throw [NSException exceptionWithName:@"InvalidByteDescriptor"
  122. reason:@"Unable to interpret bytes as a valid image."
  123. userInfo:nil];
  124. }
  125. } else {
  126. NSString* error = [NSString
  127. stringWithFormat:@"fromBytes should have exactly one argument, the bytes. Got: %lu",
  128. (unsigned long)iconData.count];
  129. NSException* exception = [NSException exceptionWithName:@"InvalidByteDescriptor"
  130. reason:error
  131. userInfo:nil];
  132. @throw exception;
  133. }
  134. }
  135. return image;
  136. }
  137. /// 检测图标相关的描述,是否修改过
  138. /// @param previousIcon 之前的图标
  139. /// @param currentIcon 当前新的图标
  140. /// @return 修改了,则返回yes,否则返回NO
  141. + (BOOL)checkIconDescriptionChangedFrom:(NSArray *)previousIcon to:(NSArray *)currentIcon {
  142. if (previousIcon.count != currentIcon.count) {
  143. return YES;
  144. }
  145. //两个数组的数量一样
  146. for (NSUInteger index = 0; index < previousIcon.count; index ++) {
  147. if ([previousIcon[index] isKindOfClass:[NSString class]]) {
  148. if ([previousIcon[index] isEqualToString:currentIcon[index]] == NO) {
  149. return YES;
  150. }
  151. } else if ([previousIcon[index] isKindOfClass:[NSNumber class]]) {
  152. if (fabs([previousIcon[index] doubleValue] - [currentIcon[index] doubleValue]) > 0.000001) {
  153. return YES;
  154. }
  155. } else {//其它数据无法比较,直接默认强制更新
  156. return NO;
  157. }
  158. }
  159. return NO;
  160. }
  161. + (BOOL)isEqualWith:(CLLocationCoordinate2D)coord1 to:(CLLocationCoordinate2D)coord2 {
  162. if (fabs(coord1.latitude - coord2.latitude) > 0.000001 || fabs(coord1.longitude - coord2.longitude) > 0.000001) {
  163. return NO;
  164. }
  165. return YES;
  166. }
  167. + (NSDictionary *)dictFromTouchPOI:(MATouchPoi *)poi {
  168. if (poi == nil) {
  169. return nil;
  170. }
  171. NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:3];
  172. if (poi.name) {
  173. [dict setObject:poi.name forKey:@"name"];
  174. }
  175. if (CLLocationCoordinate2DIsValid(poi.coordinate)) {
  176. [dict setObject:[AMapConvertUtil jsonArrayFromCoordinate:poi.coordinate] forKey:@"latLng"];
  177. }
  178. if (poi.uid) {
  179. [dict setObject:poi.uid forKey:@"id"];
  180. }
  181. return [dict copy];
  182. }
  183. + (MAMapRect)mapRectFromArray:(NSArray *)array {
  184. NSAssert((array && [array isKindOfClass:[NSArray class]] && array.count == 2), @"解析mapRect的参数有误");
  185. CLLocationCoordinate2D southwest = [AMapConvertUtil coordinateFromArray:array[0]];
  186. CLLocationCoordinate2D northeast = [AMapConvertUtil coordinateFromArray:array[1]];
  187. MAMapPoint mapNorthEastPoint = MAMapPointForCoordinate(northeast);
  188. MAMapPoint mapSouthWestPoint = MAMapPointForCoordinate(southwest);
  189. double width = fabs(mapNorthEastPoint.x - mapSouthWestPoint.x);
  190. double height = fabs(mapNorthEastPoint.y - mapSouthWestPoint.y);
  191. MAMapRect limitRect = MAMapRectMake(mapSouthWestPoint.x, mapNorthEastPoint.y, width, height);
  192. return limitRect;
  193. }
  194. @end