MAGeometry.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. /**
  232. * @brief 计算多边形面积,点与点之间按顺序尾部相连, 第一个点与最后一个点相连
  233. * @param coordinates 指定的经纬度坐标点数组,C数组,调用者负责内存管理
  234. * @param count 坐标点的个数
  235. * @return 多边形的面积
  236. */
  237. extern double MAAreaForPolygon(CLLocationCoordinate2D *coordinates, int count);
  238. static inline MAMapPoint MAMapPointMake(double x, double y)
  239. {
  240. return (MAMapPoint){x, y};
  241. }
  242. static inline MAMapSize MAMapSizeMake(double width, double height)
  243. {
  244. return (MAMapSize){width, height};
  245. }
  246. static inline MAMapRect MAMapRectMake(double x, double y, double width, double height)
  247. {
  248. return (MAMapRect){MAMapPointMake(x, y), MAMapSizeMake(width, height)};
  249. }
  250. static inline double MAMapRectGetMinX(MAMapRect rect)
  251. {
  252. return rect.origin.x;
  253. }
  254. static inline double MAMapRectGetMinY(MAMapRect rect)
  255. {
  256. return rect.origin.y;
  257. }
  258. static inline double MAMapRectGetMidX(MAMapRect rect)
  259. {
  260. return rect.origin.x + rect.size.width / 2.0;
  261. }
  262. static inline double MAMapRectGetMidY(MAMapRect rect)
  263. {
  264. return rect.origin.y + rect.size.height / 2.0;
  265. }
  266. static inline double MAMapRectGetMaxX(MAMapRect rect)
  267. {
  268. return rect.origin.x + rect.size.width;
  269. }
  270. static inline double MAMapRectGetMaxY(MAMapRect rect)
  271. {
  272. return rect.origin.y + rect.size.height;
  273. }
  274. static inline double MAMapRectGetWidth(MAMapRect rect)
  275. {
  276. return rect.size.width;
  277. }
  278. static inline double MAMapRectGetHeight(MAMapRect rect)
  279. {
  280. return rect.size.height;
  281. }
  282. static inline BOOL MAMapPointEqualToPoint(MAMapPoint point1, MAMapPoint point2) {
  283. return point1.x == point2.x && point1.y == point2.y;
  284. }
  285. static inline BOOL MAMapSizeEqualToSize(MAMapSize size1, MAMapSize size2) {
  286. return size1.width == size2.width && size1.height == size2.height;
  287. }
  288. static inline BOOL MAMapRectEqualToRect(MAMapRect rect1, MAMapRect rect2) {
  289. return
  290. MAMapPointEqualToPoint(rect1.origin, rect2.origin) &&
  291. MAMapSizeEqualToSize(rect1.size, rect2.size);
  292. }
  293. static inline BOOL MAMapRectIsNull(MAMapRect rect) {
  294. return isinf(rect.origin.x) || isinf(rect.origin.y);
  295. }
  296. static inline BOOL MAMapRectIsEmpty(MAMapRect rect) {
  297. return MAMapRectIsNull(rect) || (rect.size.width == 0.0 && rect.size.height == 0.0);
  298. }
  299. static inline NSString *MAStringFromMapPoint(MAMapPoint point) {
  300. return [NSString stringWithFormat:@"{%.1f, %.1f}", point.x, point.y];
  301. }
  302. static inline NSString *MAStringFromMapSize(MAMapSize size) {
  303. return [NSString stringWithFormat:@"{%.1f, %.1f}", size.width, size.height];
  304. }
  305. static inline NSString *MAStringFromMapRect(MAMapRect rect) {
  306. return [NSString stringWithFormat:@"{%@, %@}", MAStringFromMapPoint(rect.origin), MAStringFromMapSize(rect.size)];
  307. }
  308. ///坐标系类型枚举
  309. typedef NS_ENUM(NSUInteger, MACoordinateType)
  310. {
  311. MACoordinateTypeBaidu = 0, ///< Baidu
  312. MACoordinateTypeMapBar, ///< MapBar
  313. MACoordinateTypeMapABC, ///< MapABC
  314. MACoordinateTypeSoSoMap, ///< SoSoMap
  315. MACoordinateTypeAliYun, ///< AliYun
  316. MACoordinateTypeGoogle, ///< Google
  317. MACoordinateTypeGPS, ///< GPS
  318. };
  319. /**
  320. * @brief 转换目标经纬度为高德坐标系
  321. * @param coordinate 待转换的经纬度
  322. * @param type 坐标系类型
  323. * @return 高德坐标系经纬度
  324. */
  325. extern CLLocationCoordinate2D MACoordinateConvert(CLLocationCoordinate2D coordinate, MACoordinateType type) __attribute((deprecated("已废弃,使用AMapFoundation中关于坐标转换的接口")));
  326. /**
  327. * @brief 获取矢量坐标方向
  328. * @param fromCoord 矢量坐标起点
  329. * @param toCoord 矢量坐标终点
  330. * @return 方向,详情参考系统 CLLocationDirection
  331. */
  332. extern CLLocationDirection MAGetDirectionFromCoords(CLLocationCoordinate2D fromCoord, CLLocationCoordinate2D toCoord);
  333. /**
  334. * @brief 获取矢量坐标方向
  335. * @param fromPoint 矢量坐标起点
  336. * @param toPoint 矢量坐标终点
  337. * @return 方向,详情参考系统 CLLocationDirection
  338. */
  339. extern CLLocationDirection MAGetDirectionFromPoints(MAMapPoint fromPoint, MAMapPoint toPoint);
  340. /**
  341. * @brief 获取点到线的垂直距离
  342. * @param point 起点
  343. * @param lineBegin 线的起点
  344. * @param lineEnd 线的终点
  345. * @return 距离,单位米
  346. */
  347. extern double MAGetDistanceFromPointToLine(MAMapPoint point, MAMapPoint lineBegin, MAMapPoint lineEnd);
  348. #ifdef __cplusplus
  349. }
  350. #endif
  351. ///utils方法,方便c结构体对象和NSValue对象间相互转化
  352. @interface NSValue (NSValueMAGeometryExtensions)
  353. /**
  354. * @brief 创建 MAMapPoint 的NSValue对象
  355. * @param mapPoint MAMapPoint结构体对象
  356. * @return NSValue对象
  357. */
  358. + (NSValue *)valueWithMAMapPoint:(MAMapPoint)mapPoint;
  359. /**
  360. * @brief 创建 MAMapSize 的NSValue对象
  361. * @param mapSize MAMapSize结构体对象
  362. * @return NSValue对象
  363. */
  364. + (NSValue *)valueWithMAMapSize:(MAMapSize)mapSize;
  365. /**
  366. * @brief 创建 MAMapRect 的NSValue对象
  367. * @param mapRect MAMapRect结构体对象
  368. * @return NSValue对象
  369. */
  370. + (NSValue *)valueWithMAMapRect:(MAMapRect)mapRect;
  371. /**
  372. * @brief 创建 CLLocationCoordinate2D 的NSValue对象
  373. * @param coordinate CLLocationCoordinate2D结构体对象
  374. * @return NSValue对象
  375. */
  376. + (NSValue *)valueWithMACoordinate:(CLLocationCoordinate2D)coordinate;
  377. /**
  378. @brief 返回NSValue对象包含的MAMapPoint结构体对象
  379. @return 当前对象包含的MAMapPoint结构体对象
  380. */
  381. - (MAMapPoint)MAMapPointValue;
  382. /**
  383. @brief 返回NSValue对象包含的MAMapSize结构体对象
  384. @return 当前对象包含的MAMapSize结构体对象
  385. */
  386. - (MAMapSize)MAMapSizeValue;
  387. /**
  388. @brief 返回NSValue对象包含的MAMapRect结构体对象
  389. @return 当前对象包含的MAMapRect结构体对象
  390. */
  391. - (MAMapRect)MAMapRectValue;
  392. /**
  393. @brief 返回NSValue对象包含的CLLocationCoordinate2D结构体对象
  394. @return 当前对象包含的CLLocationCoordinate2D结构体对象
  395. */
  396. - (CLLocationCoordinate2D)MACoordinateValue;
  397. @end