开发 iOS 轻量版地图SDK 开发指南 地图计算工具 距离/面积计算

距离/面积计算 最后更新时间: 2021年03月10日

iOS 地图 SDK中的 MAGeometry.h 中提供了很多计算方法,包括:计算两点距离、矩形面积、坐标转换、判断点是否圆或者多边形内等等,下面做简单介绍,具体的接口可参考:MAGeometry.h。

1、两点间的直线距离计算

根据用户指定的两个经纬度坐标点,计算这两个点间的直线距离,单位为米。代码如下:

//1.将两个经纬度点转成投影点 
MAMapPoint point1 = MAMapPointForCoordinate(CLLocationCoordinate2DMake(39.989612,116.480972));
MAMapPoint point2 = MAMapPointForCoordinate(CLLocationCoordinate2DMake(39.990347,116.480441));
//2.计算距离
CLLocationDistance distance = MAMetersBetweenMapPoints(point1,point2);
//1.将两个经纬度点转成投影点
let point1 = MAMapPointForCoordinate(CLLocationCoordinate2D(latitude: 39.989612, longitude: 116.480972))
let point2 = MAMapPointForCoordinate(CLLocationCoordinate2D(latitude: 39.990347, longitude: 116.480441))

//2.计算距离
let distance = MAMetersBetweenMapPoints(point1,point2);

2、判断点是否在圆形内

判断一个点是否在圆内,类似于地理围栏功能,返回YES,表示进入围栏,返回NO,表示离开围栏。

CLLocationCoordinate2D location = CLLocationCoordinate2DMake(39.989612,116.480972);
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(39.990347,116.480441);
BOOL isContains = MACircleContainsCoordinate(location, center, 200);
let location = CLLocationCoordinate2D(latitude: 39.989612, longitude: 116.480972)
let center = CLLocationCoordinate2D(latitude: 39.990347, longitude: 116.480441)
let isContain = MACircleContainsCoordinate(location, center, 200)

3、判断点是否在可视范围内

判断一个用户指定的annotation是否在可视区域。代码如下:

//1.将annotation的经纬度点转成投影点
MAMapPoint point = MAMapPointForCoordinate(annotation.coordinate);
//2.判断该点是否在地图可视范围
BOOL isContains = MAMapRectContainsPoint(mapview.visibleMapRect, point);
//1.将annotation的经纬度点转成投影点
let point = MAMapPointForCoordinate(annotation.centerCoordinate)
//2.判断该点是否在地图可视范围
let isContain = MAMapRectContainsPoint(mapView.visibleMapRect, point)


返回顶部 示例中心 常见问题 智能客服 公众号
二维码