2013-05-31 82 views
2

我想计算我的位置和一些注释之间的中心点。到目前为止,我已经这样做了:如何计算CLLocationDistance的中心坐标

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”的中心点/坐标?

回答

7
#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; 
} 
+0

-1只是代码没有答案。请参阅naini关于地球是一个球体的暗示 –

+0

@ Daij-Djan哦,真的很抱歉只是代码,没有答案..其实我只是从这里http://stackoverflow.com/a/4167772/1059705实现这个代码,并认为它可能会帮助。 – Bala

+0

http://stackoverflow.com/a/4656937/1059705即使在这里,他们也做了同样的事情。@ Daij-Djan – Bala

2

我写的库函数在斯威夫特来计算多个坐标之间的中点如下:

//  /** Degrees to Radian **/ 

class func degreeToRadian(angle:CLLocationDegrees) -> CGFloat{ 

    return ( (CGFloat(angle))/180.0 * CGFloat(M_PI) ) 

} 

//  /** Radians to Degrees **/ 

class func radianToDegree(radian:CGFloat) -> CLLocationDegrees{ 

    return CLLocationDegrees( radian * CGFloat(180.0/M_PI) ) 

} 

class func middlePointOfListMarkers(listCoords: [CLLocationCoordinate2D]) -> CLLocationCoordinate2D{ 

    var x = 0.0 as CGFloat 

    var y = 0.0 as CGFloat 

    var z = 0.0 as CGFloat 



    for coordinate in listCoords{ 

     var lat:CGFloat = degreeToRadian(coordinate.latitude) 

     var lon:CGFloat = degreeToRadian(coordinate.longitude) 

     x = x + cos(lat) * cos(lon) 

     y = y + cos(lat) * sin(lon); 

     z = z + sin(lat); 

    } 

    x = x/CGFloat(listCoords.count) 

    y = y/CGFloat(listCoords.count) 

    z = z/CGFloat(listCoords.count) 



    var resultLon: CGFloat = atan2(y, x) 

    var resultHyp: CGFloat = sqrt(x*x+y*y) 

    var resultLat:CGFloat = atan2(z, resultHyp) 



    var newLat = radianToDegree(resultLat) 

    var newLon = radianToDegree(resultLon) 

    var result:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: newLat, longitude: newLon) 

    return result 

} 

详细的解答,可以发现here