24

我正在尝试确定MKMapView. 我正在按照此处(和此处)概述的方法并在 Objective-C 中重写它,但地图的中心是巴芬岛东北部的某个地方,这与两点不相近。

我的方法基于上面链接的java方法:

+(CLLocationCoordinate2D)findCenterPoint:(CLLocationCoordinate2D)_lo1 :(CLLocationCoordinate2D)_loc2 {
    CLLocationCoordinate2D center;

    double lon1 = _lo1.longitude * M_PI / 180;
    double lon2 = _loc2.longitude * M_PI / 100;

    double lat1 = _lo1.latitude * M_PI / 180;
    double lat2 = _loc2.latitude * M_PI / 100;

    double dLon = lon2 - lon1;

    double x = cos(lat2) * cos(dLon);
    double y = cos(lat2) * sin(dLon);

    double lat3 = atan2( sin(lat1) + sin(lat2), sqrt((cos(lat1) + x) * (cos(lat1) + x) + y * y) );
    double lon3 = lon1 + atan2(y, cos(lat1) + x);

    center.latitude  = lat3 * 180 / M_PI;
    center.longitude = lon3 * 180 / M_PI;

    return center;
}

2个参数有以下数据:

_loc1:
    latitude = 45.4959839
    longitude = -73.67826455

_loc2:
    latitude = 45.482889
    longitude = -73.57522299

以上内容正确放置在地图上(蒙特利尔及其周边地区)。我试图将地图居中在 2 之间的中点,但我的方法返回以下内容:

latitude = 65.29055
longitude = -82.55425

在北极的某个地方,应该在南边大约 500 英里处。

4

5 回答 5

18

如果有人需要 Swift 中的代码,我在 Swift 中编写了库函数来计算多个坐标之间的中点:

//        /** 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

}

详细答案可以在这里找到

为 Swift 5 更新

func geographicMidpoint(betweenCoordinates coordinates: [CLLocationCoordinate2D]) -> CLLocationCoordinate2D {

    guard coordinates.count > 1 else {
        return coordinates.first ?? // return the only coordinate
            CLLocationCoordinate2D(latitude: 0, longitude: 0) // return null island if no coordinates were given
    }

    var x = Double(0)
    var y = Double(0)
    var z = Double(0)

    for coordinate in coordinates {
        let lat = coordinate.latitude.toRadians()
        let lon = coordinate.longitude.toRadians()
        x += cos(lat) * cos(lon)
        y += cos(lat) * sin(lon)
        z += sin(lat)
    }

    x /= Double(coordinates.count)
    y /= Double(coordinates.count)
    z /= Double(coordinates.count)

    let lon = atan2(y, x)
    let hyp = sqrt(x * x + y * y)
    let lat = atan2(z, hyp)

    return CLLocationCoordinate2D(latitude: lat.toDegrees(), longitude: lon.toDegrees())
}

}

于 2015-05-21T03:25:02.973 回答
15

只是一种预感,但我注意到你的lon2andlat2变量是用M_PI/100and not计算的M_PI/180

double lon1 = _lo1.longitude * M_PI / 180;
double lon2 = _loc2.longitude * M_PI / 100;

double lat1 = _lo1.latitude * M_PI / 180;
double lat2 = _loc2.latitude * M_PI / 100;

将它们更改为 180 可能会对您有所帮助。

于 2012-05-11T22:19:54.383 回答
10

对于 swift 用户,按照@dinjas 的建议更正了变体

import Foundation
import MapKit
extension CLLocationCoordinate2D {
    // MARK: CLLocationCoordinate2D+MidPoint
    func middleLocationWith(location:CLLocationCoordinate2D) -> CLLocationCoordinate2D {

        let lon1 = longitude * M_PI / 180
        let lon2 = location.longitude * M_PI / 180
        let lat1 = latitude * M_PI / 180
        let lat2 = location.latitude * M_PI / 180
        let dLon = lon2 - lon1
        let x = cos(lat2) * cos(dLon)
        let y = cos(lat2) * sin(dLon)

        let lat3 = atan2( sin(lat1) + sin(lat2), sqrt((cos(lat1) + x) * (cos(lat1) + x) + y * y) )
        let lon3 = lon1 + atan2(y, cos(lat1) + x)

        let center:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat3 * 180 / M_PI, lon3 * 180 / M_PI)
        return center
    }
}
于 2016-10-12T11:55:10.763 回答
2

重要的是要说 OP 用于计算地理中点的公式是基于这个解释 cos/sin/sqrt 计算的公式。

这个公式将为您提供任何长距离的地理中点,包括四个季度和本初子午线。

但是,如果您的计算是针对1 公里左右的短程,使用简单的平均值将产生相同的中点结果。IE:

let firstPoint = CLLocation(....)
let secondPoint = CLLocation(....)

let midPointLat = (firstPoint.coordinate.latitude + secondPoint.coordinate.latitude) / 2
let midPointLong = (firstPoint.coordinate.longitude + secondPoint.coordinate.longitude) / 2

您实际上可以将它用于 10 公里,但预计会出现偏差 - 如果您只需要使用快速解决方案对短程中点进行估计,那就足够了。

于 2018-04-09T11:06:44.377 回答
-10

我觉得你想多了。做就是了:

float lon3 = ((lon1 + lon2) / 2)
float lat3 = ((lat1 + lat2) / 2)

lat3 和 lon3 将是中心点。

于 2012-05-11T22:24:27.983 回答