You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

491 lines
16 KiB

//
// MAGeometry.h
// MAMapKit
//
// Created by AutoNavi.
// Copyright (c) 2013年 Amap. All rights reserved.
//
#import "MAConfig.h"
#import <CoreGraphics/CoreGraphics.h>
#import <CoreLocation/CoreLocation.h>
#import <UIKit/UIKit.h>
#ifdef __cplusplus
extern "C" {
#endif
///东北、西南两个点定义的四边形经纬度范围
typedef struct MACoordinateBounds{
CLLocationCoordinate2D northEast; ///< 东北角经纬度
CLLocationCoordinate2D southWest; ///< 西南角经纬度
} MACoordinateBounds;
///经度、纬度定义的经纬度跨度范围
typedef struct MACoordinateSpan{
CLLocationDegrees latitudeDelta; ///< 纬度跨度
CLLocationDegrees longitudeDelta; ///< 经度跨度
} MACoordinateSpan;
///中心点、跨度范围定义的四边形经纬度范围
typedef struct MACoordinateRegion{
CLLocationCoordinate2D center; ///< 中心点经纬度
MACoordinateSpan span; ///< 跨度范围
} MACoordinateRegion;
///平面投影坐标结构定义
typedef struct MAMapPoint{
double x; ///<x坐标
double y; ///<y坐标
} MAMapPoint;
///平面投影大小结构定义
typedef struct MAMapSize{
double width; ///<宽度
double height; ///<高度
} MAMapSize;
///平面投影矩形结构定义
typedef struct MAMapRect{
MAMapPoint origin; ///<左上角坐标
MAMapSize size; ///<大小
} MAMapRect;
typedef NS_OPTIONS(NSUInteger, MAMapRectCorner) {
MAMapRectCornerTopLeft = 1 << 0,
MAMapRectCornerTopRight = 1 << 1,
MAMapRectCornerBottomLeft = 1 << 2,
MAMapRectCornerBottomRight = 1 << 3,
MAMapRectCornerAllCorners = ~0UL
};
///比例关系:MAZoomScale = Screen Point / MAMapPoint, 当MAZoomScale = 1时, 1 screen point = 1 MAMapPoint, 当MAZoomScale = 0.5时, 1 screen point = 2 MAMapPoints
typedef double MAZoomScale;
///世界范围大小
extern const MAMapSize MAMapSizeWorld;
///世界范围四边形
extern const MAMapRect MAMapRectWorld;
///(MAMapRect){{INFINITY, INFINITY}, {0, 0}};
extern const MAMapRect MAMapRectNull;
///(MAMapRect){{0, 0}, {0, 0}}
extern const MAMapRect MAMapRectZero;
static inline MACoordinateBounds MACoordinateBoundsMake(CLLocationCoordinate2D northEast,CLLocationCoordinate2D southWest)
{
return (MACoordinateBounds){northEast, southWest};
}
static inline MACoordinateSpan MACoordinateSpanMake(CLLocationDegrees latitudeDelta, CLLocationDegrees longitudeDelta)
{
return (MACoordinateSpan){latitudeDelta, longitudeDelta};
}
static inline MACoordinateRegion MACoordinateRegionMake(CLLocationCoordinate2D centerCoordinate, MACoordinateSpan span)
{
return (MACoordinateRegion){centerCoordinate, span};
}
/**
* @brief MACoordinateRegion
* @param centerCoordinate
* @param latitudinalMeters ( )
* @param longitudinalMeters ( )
* @return MACoordinateRegion
*/
extern MACoordinateRegion MACoordinateRegionMakeWithDistance(CLLocationCoordinate2D centerCoordinate, CLLocationDistance latitudinalMeters, CLLocationDistance longitudinalMeters);
/**
* @brief
* @param coordinate
* @return
*/
extern MAMapPoint MAMapPointForCoordinate(CLLocationCoordinate2D coordinate);
/**
* @brief
* @param mapPoint
* @return
*/
extern CLLocationCoordinate2D MACoordinateForMapPoint(MAMapPoint mapPoint);
/**
* @brief region
* @param rect
* @return region
*/
extern MACoordinateRegion MACoordinateRegionForMapRect(MAMapRect rect);
/**
* @brief region转平面投影矩形
* @param region region region
* @return
*/
extern MAMapRect MAMapRectForCoordinateRegion(MACoordinateRegion region);
/**
* @brief
* @param latitude
* @return
*/
extern CLLocationDistance MAMetersPerMapPointAtLatitude(CLLocationDegrees latitude);
/**
* @brief 1
* @param latitude
* @return 1
*/
extern double MAMapPointsPerMeterAtLatitude(CLLocationDegrees latitude);
/**
* @brief
* @param a a点
* @param b b点
* @return
*/
extern CLLocationDistance MAMetersBetweenMapPoints(MAMapPoint a, MAMapPoint b);
/**
* @brief ( )
* @param northEast
* @param southWest 西
* @return
*/
extern double MAAreaBetweenCoordinates(CLLocationCoordinate2D northEast, CLLocationCoordinate2D southWest);
/**
* @brief Inset后的MAMapRect
* @param rect rect
* @param dx x点
* @param dy y点
* @return MAMapRect
*/
extern MAMapRect MAMapRectInset(MAMapRect rect, double dx, double dy);
/**
* @brief MAMapRect
* @param rect1 rect1
* @param rect2 rect2
* @return rect
*/
extern MAMapRect MAMapRectUnion(MAMapRect rect1, MAMapRect rect2);
/**
* @brief size1是否包含size2
* @param size1 size1
* @param size2 size2
* @return
*/
extern BOOL MAMapSizeContainsSize(MAMapSize size1, MAMapSize size2);
/**
* @brief
* @param rect rect
* @param point
* @return
*/
extern BOOL MAMapRectContainsPoint(MAMapRect rect, MAMapPoint point);
/**
* @brief
* @param rect1 rect1
* @param rect2 rect2
* @return
*/
extern BOOL MAMapRectIntersectsRect(MAMapRect rect1, MAMapRect rect2);
/**
* @brief rect1是否包含矩形rect2
* @param rect1 rect1
* @param rect2 rect2
* @return
*/
extern BOOL MAMapRectContainsRect(MAMapRect rect1, MAMapRect rect2);
/**
* @brief
* @param point
* @param center
* @param radius
* @return
*/
extern BOOL MACircleContainsPoint(MAMapPoint point, MAMapPoint center, double radius);
/**
* @brief
* @param point
* @param center
* @param radius
* @return
*/
extern BOOL MACircleContainsCoordinate(CLLocationCoordinate2D point, CLLocationCoordinate2D center, double radius);
/**
* @brief 线
* @param point
* @param polyline 线
* @param count 线
* @return 线
*/
extern MAMapPoint MAGetNearestMapPointFromPolyline(MAMapPoint point, MAMapPoint *polyline, NSUInteger count);
/**
* @brief
* @param point
* @param polygon
* @param count
* @return
*/
extern BOOL MAPolygonContainsPoint(MAMapPoint point, MAMapPoint *polygon, NSUInteger count);
/**
* @brief
* @param point
* @param polygon
* @param count
* @return
*/
extern BOOL MAPolygonContainsCoordinate(CLLocationCoordinate2D point, CLLocationCoordinate2D *polygon, NSUInteger count);
/**
* @brief lineStart和lineEnd组成的线段上距离point距离最近的点
* @param lineStart 线
* @param lineEnd 线
* @param point
* @return point最近的点坐标
*/
extern MAMapPoint MAGetNearestMapPointFromLine(MAMapPoint lineStart, MAMapPoint lineEnd, MAMapPoint point);
/**
* @brief block(-1, -1, 0, 0, 0, 0)
* @param offsetX tile的位移X,
* @param offsetY tile的位移Y,
* @param minX tile的最小x
* @param maxX tile的最大x
* @param minY tile的最小y
* @param maxY tile的最大y
*/
typedef void (^AMapTileProjectionBlock)(int offsetX, int offsetY, int minX, int maxX, int minY, int maxY);
/**
* @brief
* @param bounds
* @param levelOfDetails , 0-20
* @param tileProjection block
*/
extern void MAGetTileProjectionFromBounds(MACoordinateBounds bounds, int levelOfDetails, AMapTileProjectionBlock tileProjection);
/**
* @brief ,
* @param coordinates C数组
* @param count
* @return
*/
extern double MAAreaForPolygon(CLLocationCoordinate2D *coordinates, int count);
static inline MAMapPoint MAMapPointMake(double x, double y)
{
return (MAMapPoint){x, y};
}
static inline MAMapSize MAMapSizeMake(double width, double height)
{
return (MAMapSize){width, height};
}
static inline MAMapRect MAMapRectMake(double x, double y, double width, double height)
{
return (MAMapRect){MAMapPointMake(x, y), MAMapSizeMake(width, height)};
}
static inline double MAMapRectGetMinX(MAMapRect rect)
{
return rect.origin.x;
}
static inline double MAMapRectGetMinY(MAMapRect rect)
{
return rect.origin.y;
}
static inline double MAMapRectGetMidX(MAMapRect rect)
{
return rect.origin.x + rect.size.width / 2.0;
}
static inline double MAMapRectGetMidY(MAMapRect rect)
{
return rect.origin.y + rect.size.height / 2.0;
}
static inline double MAMapRectGetMaxX(MAMapRect rect)
{
return rect.origin.x + rect.size.width;
}
static inline double MAMapRectGetMaxY(MAMapRect rect)
{
return rect.origin.y + rect.size.height;
}
static inline double MAMapRectGetWidth(MAMapRect rect)
{
return rect.size.width;
}
static inline double MAMapRectGetHeight(MAMapRect rect)
{
return rect.size.height;
}
static inline BOOL MAMapPointEqualToPoint(MAMapPoint point1, MAMapPoint point2) {
return point1.x == point2.x && point1.y == point2.y;
}
static inline BOOL MAMapSizeEqualToSize(MAMapSize size1, MAMapSize size2) {
return size1.width == size2.width && size1.height == size2.height;
}
static inline BOOL MAMapRectEqualToRect(MAMapRect rect1, MAMapRect rect2) {
return
MAMapPointEqualToPoint(rect1.origin, rect2.origin) &&
MAMapSizeEqualToSize(rect1.size, rect2.size);
}
static inline BOOL MAMapRectIsNull(MAMapRect rect) {
return isinf(rect.origin.x) || isinf(rect.origin.y);
}
static inline BOOL MAMapRectIsEmpty(MAMapRect rect) {
return MAMapRectIsNull(rect) || (rect.size.width == 0.0 && rect.size.height == 0.0);
}
static inline NSString *MAStringFromMapPoint(MAMapPoint point) {
return [NSString stringWithFormat:@"{%.1f, %.1f}", point.x, point.y];
}
static inline NSString *MAStringFromMapSize(MAMapSize size) {
return [NSString stringWithFormat:@"{%.1f, %.1f}", size.width, size.height];
}
static inline NSString *MAStringFromMapRect(MAMapRect rect) {
return [NSString stringWithFormat:@"{%@, %@}", MAStringFromMapPoint(rect.origin), MAStringFromMapSize(rect.size)];
}
///坐标系类型枚举
typedef NS_ENUM(NSUInteger, MACoordinateType)
{
MACoordinateTypeBaidu = 0, ///< Baidu
MACoordinateTypeMapBar, ///< MapBar
MACoordinateTypeMapABC, ///< MapABC
MACoordinateTypeSoSoMap, ///< SoSoMap
MACoordinateTypeAliYun, ///< AliYun
MACoordinateTypeGoogle, ///< Google
MACoordinateTypeGPS, ///< GPS
};
/**
* @brief
* @param coordinate
* @param type
* @return
*/
extern CLLocationCoordinate2D MACoordinateConvert(CLLocationCoordinate2D coordinate, MACoordinateType type) __attribute((deprecated("已废弃,使用AMapFoundation中关于坐标转换的接口")));
/**
* @brief
* @param fromCoord
* @param toCoord
* @return CLLocationDirection
*/
extern CLLocationDirection MAGetDirectionFromCoords(CLLocationCoordinate2D fromCoord, CLLocationCoordinate2D toCoord);
/**
* @brief
* @param fromPoint
* @param toPoint
* @return CLLocationDirection
*/
extern CLLocationDirection MAGetDirectionFromPoints(MAMapPoint fromPoint, MAMapPoint toPoint);
/**
* @brief 线
* @param point
* @param lineBegin 线
* @param lineEnd 线
* @return
*/
extern double MAGetDistanceFromPointToLine(MAMapPoint point, MAMapPoint lineBegin, MAMapPoint lineEnd);
/**
* @brief 线
* @param linePoints 线
* @param count
* @param tappedPoint
* @param lineWidth 线MAMapPoint点
* @return
*/
extern BOOL MAPolylineHitTest(MAMapPoint *linePoints, NSUInteger count, MAMapPoint tappedPoint, CGFloat lineWidth);
#ifdef __cplusplus
}
#endif
///utils方法,方便c结构体对象和NSValue对象间相互转化
@interface NSValue (NSValueMAGeometryExtensions)
/**
* @brief MAMapPoint NSValue对象
* @param mapPoint MAMapPoint结构体对象
* @return NSValue对象
*/
+ (NSValue *)valueWithMAMapPoint:(MAMapPoint)mapPoint;
/**
* @brief MAMapSize NSValue对象
* @param mapSize MAMapSize结构体对象
* @return NSValue对象
*/
+ (NSValue *)valueWithMAMapSize:(MAMapSize)mapSize;
/**
* @brief MAMapRect NSValue对象
* @param mapRect MAMapRect结构体对象
* @return NSValue对象
*/
+ (NSValue *)valueWithMAMapRect:(MAMapRect)mapRect;
/**
* @brief CLLocationCoordinate2D NSValue对象
* @param coordinate CLLocationCoordinate2D结构体对象
* @return NSValue对象
*/
+ (NSValue *)valueWithMACoordinate:(CLLocationCoordinate2D)coordinate;
/**
@brief NSValue对象包含的MAMapPoint结构体对象
@return MAMapPoint结构体对象
*/
- (MAMapPoint)MAMapPointValue;
/**
@brief NSValue对象包含的MAMapSize结构体对象
@return MAMapSize结构体对象
*/
- (MAMapSize)MAMapSizeValue;
/**
@brief NSValue对象包含的MAMapRect结构体对象
@return MAMapRect结构体对象
*/
- (MAMapRect)MAMapRectValue;
/**
@brief NSValue对象包含的CLLocationCoordinate2D结构体对象
@return CLLocationCoordinate2D结构体对象
*/
- (CLLocationCoordinate2D)MACoordinateValue;
@end