MAGeometry.h 16 KB

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