MAGeometry.h.svn-base 14 KB

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