版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1337754
我们在使用到百度地图的项目中可能会需要计算两点间的距离,我们可以很容易通过百度地图的开发文档中找到计算两点间距离的方法:
BMKMapPoint point1 = BMKMapPointForCoordinate(userLocation.location.coordinate);
BMKMapPoint point2 = BMKMapPointForCoordinate(CLLocationCoordinate2DMake(model.latitude,model.longitude));
CLLocationDistance distance = BMKMetersBetweenMapPoints(point1,point2);
可该代码不能直接使用,因为我们还需要导入一个头文件,反正我在百度地图开发文档中没有直接找到该头文件(可能找的不够仔细)。最后是在iOS技术交流群被告知了这个头文件:
BaiduMapAPI_Utils/BMKUtilsComponent.h
计算出的距离单位是米。
在没找到该头文件前我本来是想放弃百度地图的这个方法而使用原生的方法的,原生的方法:
CLLocation *location1 = [CLLocation alloc initWithLatitude:userLocation.location.coordinate.latitude longitude:userLocation.location.coordinate.longitude];
CLLocation *location2 = [CLLocation alloc initWithLatitude:model.latitude longitude:model.longitude] ;
CLLocationDistance distance = location1 distanceFromLocation:location2;