本文介绍了如何计算CLLocationDistance的中心坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想计算我的位置和一些注释之间的中心点。到目前为止,我已经这样做了:
I want calculate the center point between my location and some annotation. So far I have done this:
CLLocation *myLoc = self.locMgr.location;
MKPointAnnotation *middleAnnotation = [locationV.annotations objectAtIndex:locationV.annotations.count/2];
CLLocation *someStuiodLoc = [[CLLocation alloc] initWithLatitude:middleAnnotation.coordinate.latitude longitude:middleAnnotation.coordinate.longitude];
CLLocationDistance dist = [myLoc distanceFromLocation:someStuiodLoc];
如何计算 dist的中心点/坐标?
How can I calculate the center point/cordinate of "dist" ??
推荐答案
#define ToRadian(x) ((x) * M_PI/180)
#define ToDegrees(x) ((x) * 180/M_PI)
+ (CLLocationCoordinate2D)midpointBetweenCoordinate:(CLLocationCoordinate2D)c1 andCoordinate:(CLLocationCoordinate2D)c2
{
c1.latitude = ToRadian(c1.latitude);
c2.latitude = ToRadian(c2.latitude);
CLLocationDegrees dLon = ToRadian(c2.longitude - c1.longitude);
CLLocationDegrees bx = cos(c2.latitude) * cos(dLon);
CLLocationDegrees by = cos(c2.latitude) * sin(dLon);
CLLocationDegrees latitude = atan2(sin(c1.latitude) + sin(c2.latitude), sqrt((cos(c1.latitude) + bx) * (cos(c1.latitude) + bx) + by*by));
CLLocationDegrees longitude = ToRadian(c1.longitude) + atan2(by, cos(c1.latitude) + bx);
CLLocationCoordinate2D midpointCoordinate;
midpointCoordinate.longitude = ToDegrees(longitude);
midpointCoordinate.latitude = ToDegrees(latitude);
return midpointCoordinate;
}
这篇关于如何计算CLLocationDistance的中心坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!