MAGeometry.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. //
  2. // MAGeometry.h
  3. // MAMapKit
  4. //
  5. // Created by AutoNavi.
  6. // Copyright (c) 2013年 Amap. All rights reserved.
  7. //
  8. #import "MAConfig.h"
  9. #import <CoreGraphics/CoreGraphics.h>
  10. #import <CoreLocation/CoreLocation.h>
  11. #import <UIKit/UIKit.h>
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. ///东北、西南两个点定义的四边形经纬度范围
  16. struct MACoordinateBounds{
  17. CLLocationCoordinate2D northEast; ///< 东北角经纬度
  18. CLLocationCoordinate2D southWest; ///< 西南角经纬度
  19. };
  20. typedef struct MACoordinateBounds MACoordinateBounds;
  21. ///经度、纬度定义的经纬度跨度范围
  22. struct MACoordinateSpan{
  23. CLLocationDegrees latitudeDelta; ///< 纬度跨度
  24. CLLocationDegrees longitudeDelta; ///< 经度跨度
  25. };
  26. typedef struct MACoordinateSpan MACoordinateSpan;
  27. ///中心点、跨度范围定义的四边形经纬度范围
  28. struct MACoordinateRegion{
  29. CLLocationCoordinate2D center; ///< 中心点经纬度
  30. MACoordinateSpan span; ///< 跨度范围
  31. };
  32. typedef struct MACoordinateRegion MACoordinateRegion;
  33. ///平面投影坐标结构定义
  34. struct MAMapPoint{
  35. double x; ///<x坐标
  36. double y; ///<y坐标
  37. };
  38. typedef struct MAMapPoint MAMapPoint;
  39. ///平面投影大小结构定义
  40. struct MAMapSize{
  41. double width; ///<宽度
  42. double height; ///<高度
  43. };
  44. typedef struct MAMapSize MAMapSize;
  45. ///平面投影矩形结构定义
  46. struct MAMapRect{
  47. MAMapPoint origin; ///<左上角坐标
  48. MAMapSize size; ///<大小
  49. };
  50. typedef struct MAMapRect MAMapRect;
  51. ///比例关系:MAZoomScale = Screen Point / MAMapPoint, 当MAZoomScale = 1时, 1 screen point = 1 MAMapPoint, 当MAZoomScale = 0.5时, 1 screen point = 2 MAMapPoints
  52. typedef double MAZoomScale;
  53. ///世界范围大小
  54. extern const MAMapSize MAMapSizeWorld;
  55. ///世界范围四边形
  56. extern const MAMapRect MAMapRectWorld;
  57. ///(MAMapRect){{INFINITY, INFINITY}, {0, 0}};
  58. extern const MAMapRect MAMapRectNull;
  59. ///(MAMapRect){{0, 0}, {0, 0}}
  60. extern const MAMapRect MAMapRectZero;
  61. static inline MACoordinateBounds MACoordinateBoundsMake(CLLocationCoordinate2D northEast,CLLocationCoordinate2D southWest)
  62. {
  63. return (MACoordinateBounds){northEast, southWest};
  64. }
  65. static inline MACoordinateSpan MACoordinateSpanMake(CLLocationDegrees latitudeDelta, CLLocationDegrees longitudeDelta)
  66. {
  67. return (MACoordinateSpan){latitudeDelta, longitudeDelta};
  68. }
  69. static inline MACoordinateRegion MACoordinateRegionMake(CLLocationCoordinate2D centerCoordinate, MACoordinateSpan span)
  70. {
  71. return (MACoordinateRegion){centerCoordinate, span};
  72. }
  73. /**
  74. * @brief 生成一个新的MACoordinateRegion
  75. * @param centerCoordinate 中心点坐标
  76. * @param latitudinalMeters 垂直跨度(单位 米)
  77. * @param longitudinalMeters 水平跨度(单位 米)
  78. * @return 新的MACoordinateRegion
  79. */
  80. extern MACoordinateRegion MACoordinateRegionMakeWithDistance(CLLocationCoordinate2D centerCoordinate, CLLocationDistance latitudinalMeters, CLLocationDistance longitudinalMeters);
  81. /**
  82. * @brief 经纬度坐标转平面投影坐标
  83. * @param coordinate 要转化的经纬度坐标
  84. * @return 平面投影坐标
  85. */
  86. extern MAMapPoint MAMapPointForCoordinate(CLLocationCoordinate2D coordinate);
  87. /**
  88. * @brief 平面投影坐标转经纬度坐标
  89. * @param mapPoint 要转化的平面投影坐标
  90. * @return 经纬度坐标
  91. */
  92. extern CLLocationCoordinate2D MACoordinateForMapPoint(MAMapPoint mapPoint);
  93. /**
  94. * @brief 平面投影矩形转region
  95. * @param rect 要转化的平面投影矩形
  96. * @return region
  97. */
  98. extern MACoordinateRegion MACoordinateRegionForMapRect(MAMapRect rect);
  99. /**
  100. * @brief region转平面投影矩形
  101. * @param region region 要转化的region
  102. * @return 平面投影矩形
  103. */
  104. extern MAMapRect MAMapRectForCoordinateRegion(MACoordinateRegion region);
  105. /**
  106. * @brief 单位投影的距离
  107. * @param latitude 经纬度
  108. * @return 距离
  109. */
  110. extern CLLocationDistance MAMetersPerMapPointAtLatitude(CLLocationDegrees latitude);
  111. /**
  112. * @brief 1米对应的投影
  113. * @param latitude 经纬度
  114. * @return 1米对应的投影
  115. */
  116. extern double MAMapPointsPerMeterAtLatitude(CLLocationDegrees latitude);
  117. /**
  118. * @brief 投影两点之间的距离
  119. * @param a a点
  120. * @param b b点
  121. * @return 距离
  122. */
  123. extern CLLocationDistance MAMetersBetweenMapPoints(MAMapPoint a, MAMapPoint b);
  124. /**
  125. * @brief 经纬度间的面积(单位 平方米)
  126. * @param northEast 东北经纬度
  127. * @param southWest 西南经纬度
  128. * @return 面积
  129. */
  130. extern double MAAreaBetweenCoordinates(CLLocationCoordinate2D northEast, CLLocationCoordinate2D southWest);
  131. /**
  132. * @brief 获取Inset后的MAMapRect
  133. * @param rect rect
  134. * @param dx x点
  135. * @param dy y点
  136. * @return MAMapRect
  137. */
  138. extern MAMapRect MAMapRectInset(MAMapRect rect, double dx, double dy);
  139. /**
  140. * @brief 合并两个MAMapRect
  141. * @param rect1 rect1
  142. * @param rect2 rect2
  143. * @return 合并后的rect
  144. */
  145. extern MAMapRect MAMapRectUnion(MAMapRect rect1, MAMapRect rect2);
  146. /**
  147. * @brief 判断size1是否包含size2
  148. * @param size1 size1
  149. * @param size2 size2
  150. * @return 判断结果
  151. */
  152. extern BOOL MAMapSizeContainsSize(MAMapSize size1, MAMapSize size2);
  153. /**
  154. * @brief 判断点是否在矩形内
  155. * @param rect 矩形rect
  156. * @param point 点
  157. * @return 判断结果
  158. */
  159. extern BOOL MAMapRectContainsPoint(MAMapRect rect, MAMapPoint point);
  160. /**
  161. * @brief 判断两矩形是否相交
  162. * @param rect1 rect1
  163. * @param rect2 rect2
  164. * @return 判断结果
  165. */
  166. extern BOOL MAMapRectIntersectsRect(MAMapRect rect1, MAMapRect rect2);
  167. /**
  168. * @brief 判断矩形rect1是否包含矩形rect2
  169. * @param rect1 rect1
  170. * @param rect2 rect2
  171. * @return 判断结果
  172. */
  173. extern BOOL MAMapRectContainsRect(MAMapRect rect1, MAMapRect rect2);
  174. /**
  175. * @brief 判断点是否在圆内
  176. * @param point 点
  177. * @param center 圆的中心点
  178. * @param radius 圆的半径
  179. * @return 判断结果
  180. */
  181. extern BOOL MACircleContainsPoint(MAMapPoint point, MAMapPoint center, double radius);
  182. /**
  183. * @brief 判断经纬度点是否在圆内
  184. * @param point 经纬度
  185. * @param center 圆的中心经纬度
  186. * @param radius 圆的半径
  187. * @return 判断结果
  188. */
  189. extern BOOL MACircleContainsCoordinate(CLLocationCoordinate2D point, CLLocationCoordinate2D center, double radius);
  190. /**
  191. * @brief 判断点是否在多边形内
  192. * @param point 点
  193. * @param polygon 多边形
  194. * @param count 多边形点的数量
  195. * @return 判断结果
  196. */
  197. extern BOOL MAPolygonContainsPoint(MAMapPoint point, MAMapPoint *polygon, NSUInteger count);
  198. /**
  199. * @brief 判断经纬度点是否在多边形内
  200. * @param point 经纬度点
  201. * @param polygon 多边形
  202. * @param count 多边形点的数量
  203. * @return 判断结果
  204. */
  205. extern BOOL MAPolygonContainsCoordinate(CLLocationCoordinate2D point, CLLocationCoordinate2D *polygon, NSUInteger count);
  206. /**
  207. * @brief 取在lineStart和lineEnd组成的线段上距离point距离最近的点
  208. * @param lineStart 线段起点
  209. * @param lineEnd 线段终点
  210. * @param point 测试点
  211. * @return 距离point最近的点坐标
  212. */
  213. extern MAMapPoint MAGetNearestMapPointFromLine(MAMapPoint lineStart, MAMapPoint lineEnd, MAMapPoint point);
  214. /**
  215. * @brief 获取墨卡托投影切块回调block,如果是无效的映射,则返回(-1, -1, 0, 0, 0, 0)
  216. * @param offsetX 左上点距离所属tile的位移X, 单位像素
  217. * @param offsetY 左上点距离所属tile的位移Y, 单位像素
  218. * @param minX 覆盖tile的最小x
  219. * @param maxX 覆盖tile的最大x
  220. * @param minY 覆盖tile的最小y
  221. * @param maxY 覆盖tile的最大y
  222. */
  223. typedef void (^AMapTileProjectionBlock)(int offsetX, int offsetY, int minX, int maxX, int minY, int maxY);
  224. /**
  225. * @brief 根据所给经纬度区域获取墨卡托投影切块信息
  226. * @param bounds 经纬度区域
  227. * @param levelOfDetails 对应缩放级别, 取值0-20
  228. * @param tileProjection 返回的切块信息block
  229. */
  230. extern void MAGetTileProjectionFromBounds(MACoordinateBounds bounds, int levelOfDetails, AMapTileProjectionBlock tileProjection);
  231. static inline MAMapPoint MAMapPointMake(double x, double y)
  232. {
  233. return (MAMapPoint){x, y};
  234. }
  235. static inline MAMapSize MAMapSizeMake(double width, double height)
  236. {
  237. return (MAMapSize){width, height};
  238. }
  239. static inline MAMapRect MAMapRectMake(double x, double y, double width, double height)
  240. {
  241. return (MAMapRect){MAMapPointMake(x, y), MAMapSizeMake(width, height)};
  242. }
  243. static inline double MAMapRectGetMinX(MAMapRect rect)
  244. {
  245. return rect.origin.x;
  246. }
  247. static inline double MAMapRectGetMinY(MAMapRect rect)
  248. {
  249. return rect.origin.y;
  250. }
  251. static inline double MAMapRectGetMidX(MAMapRect rect)
  252. {
  253. return rect.origin.x + rect.size.width / 2.0;
  254. }
  255. static inline double MAMapRectGetMidY(MAMapRect rect)
  256. {
  257. return rect.origin.y + rect.size.height / 2.0;
  258. }
  259. static inline double MAMapRectGetMaxX(MAMapRect rect)
  260. {
  261. return rect.origin.x + rect.size.width;
  262. }
  263. static inline double MAMapRectGetMaxY(MAMapRect rect)
  264. {
  265. return rect.origin.y + rect.size.height;
  266. }
  267. static inline double MAMapRectGetWidth(MAMapRect rect)
  268. {
  269. return rect.size.width;
  270. }
  271. static inline double MAMapRectGetHeight(MAMapRect rect)
  272. {
  273. return rect.size.height;
  274. }
  275. static inline BOOL MAMapPointEqualToPoint(MAMapPoint point1, MAMapPoint point2) {
  276. return point1.x == point2.x && point1.y == point2.y;
  277. }
  278. static inline BOOL MAMapSizeEqualToSize(MAMapSize size1, MAMapSize size2) {
  279. return size1.width == size2.width && size1.height == size2.height;
  280. }
  281. static inline BOOL MAMapRectEqualToRect(MAMapRect rect1, MAMapRect rect2) {
  282. return
  283. MAMapPointEqualToPoint(rect1.origin, rect2.origin) &&
  284. MAMapSizeEqualToSize(rect1.size, rect2.size);
  285. }
  286. static inline BOOL MAMapRectIsNull(MAMapRect rect) {
  287. return isinf(rect.origin.x) || isinf(rect.origin.y);
  288. }
  289. static inline BOOL MAMapRectIsEmpty(MAMapRect rect) {
  290. return MAMapRectIsNull(rect) || (rect.size.width == 0.0 && rect.size.height == 0.0);
  291. }
  292. static inline NSString *MAStringFromMapPoint(MAMapPoint point) {
  293. return [NSString stringWithFormat:@"{%.1f, %.1f}", point.x, point.y];
  294. }
  295. static inline NSString *MAStringFromMapSize(MAMapSize size) {
  296. return [NSString stringWithFormat:@"{%.1f, %.1f}", size.width, size.height];
  297. }
  298. static inline NSString *MAStringFromMapRect(MAMapRect rect) {
  299. return [NSString stringWithFormat:@"{%@, %@}", MAStringFromMapPoint(rect.origin), MAStringFromMapSize(rect.size)];
  300. }
  301. ///坐标系类型枚举
  302. typedef NS_ENUM(NSUInteger, MACoordinateType)
  303. {
  304. MACoordinateTypeBaidu = 0, ///< Baidu
  305. MACoordinateTypeMapBar, ///< MapBar
  306. MACoordinateTypeMapABC, ///< MapABC
  307. MACoordinateTypeSoSoMap, ///< SoSoMap
  308. MACoordinateTypeAliYun, ///< AliYun
  309. MACoordinateTypeGoogle, ///< Google
  310. MACoordinateTypeGPS, ///< GPS
  311. };
  312. /**
  313. * @brief 转换目标经纬度为高德坐标系
  314. * @param coordinate 待转换的经纬度
  315. * @param type 坐标系类型
  316. * @return 高德坐标系经纬度
  317. */
  318. extern CLLocationCoordinate2D MACoordinateConvert(CLLocationCoordinate2D coordinate, MACoordinateType type) __attribute((deprecated("已废弃,使用AMapFoundation中关于坐标转换的接口")));
  319. /**
  320. * @brief 获取矢量坐标方向
  321. * @param fromCoord 矢量坐标起点
  322. * @param toCoord 矢量坐标终点
  323. * @return 方向,详情参考系统 CLLocationDirection
  324. */
  325. extern CLLocationDirection MAGetDirectionFromCoords(CLLocationCoordinate2D fromCoord, CLLocationCoordinate2D toCoord);
  326. /**
  327. * @brief 获取矢量坐标方向
  328. * @param fromPoint 矢量坐标起点
  329. * @param toPoint 矢量坐标终点
  330. * @return 方向,详情参考系统 CLLocationDirection
  331. */
  332. extern CLLocationDirection MAGetDirectionFromPoints(MAMapPoint fromPoint, MAMapPoint toPoint);
  333. /**
  334. * @brief 获取点到线的垂直距离
  335. * @param point 起点
  336. * @param lineBegin 线的起点
  337. * @param lineEnd 线的终点
  338. * @return 距离,单位米
  339. */
  340. extern double MAGetDistanceFromPointToLine(MAMapPoint point, MAMapPoint lineBegin, MAMapPoint lineEnd);
  341. #ifdef __cplusplus
  342. }
  343. #endif
  344. ///utils方法,方便c结构体对象和NSValue对象间相互转化
  345. @interface NSValue (NSValueMAGeometryExtensions)
  346. /**
  347. * @brief 创建 MAMapPoint 的NSValue对象
  348. * @param mapPoint MAMapPoint结构体对象
  349. * @return NSValue对象
  350. */
  351. + (NSValue *)valueWithMAMapPoint:(MAMapPoint)mapPoint;
  352. /**
  353. * @brief 创建 MAMapSize 的NSValue对象
  354. * @param mapSize MAMapSize结构体对象
  355. * @return NSValue对象
  356. */
  357. + (NSValue *)valueWithMAMapSize:(MAMapSize)mapSize;
  358. /**
  359. * @brief 创建 MAMapRect 的NSValue对象
  360. * @param mapRect MAMapRect结构体对象
  361. * @return NSValue对象
  362. */
  363. + (NSValue *)valueWithMAMapRect:(MAMapRect)mapRect;
  364. /**
  365. * @brief 创建 CLLocationCoordinate2D 的NSValue对象
  366. * @param coordinate CLLocationCoordinate2D结构体对象
  367. * @return NSValue对象
  368. */
  369. + (NSValue *)valueWithMACoordinate:(CLLocationCoordinate2D)coordinate;
  370. /**
  371. @brief 返回NSValue对象包含的MAMapPoint结构体对象
  372. @return 当前对象包含的MAMapPoint结构体对象
  373. */
  374. - (MAMapPoint)MAMapPointValue;
  375. /**
  376. @brief 返回NSValue对象包含的MAMapSize结构体对象
  377. @return 当前对象包含的MAMapSize结构体对象
  378. */
  379. - (MAMapSize)MAMapSizeValue;
  380. /**
  381. @brief 返回NSValue对象包含的MAMapRect结构体对象
  382. @return 当前对象包含的MAMapRect结构体对象
  383. */
  384. - (MAMapRect)MAMapRectValue;
  385. /**
  386. @brief 返回NSValue对象包含的CLLocationCoordinate2D结构体对象
  387. @return 当前对象包含的CLLocationCoordinate2D结构体对象
  388. */
  389. - (CLLocationCoordinate2D)MACoordinateValue;
  390. @end