Mapbox iOS SDK 创建曲线 MGLPolyline 和 MKGeodesicPolyline 搞得有些奇怪

Mapbox iOS SDK create a curve MGLPolyline with MKGeodesicPolyline get some strange

我尝试使用 Mapbox SDK for iOS 在地图视图上创建曲线 MGLPolyline,代码如下,

func lineBetweenLocation(location1: CLLocation, location2: CLLocation)
{   
    var points: [CLLocationCoordinate2D]
    points = [location1.coordinate, location2.coordinate]
    let geodesic = MKGeodesicPolyline(coordinates: &points, count:2)
    let line = MGLPolyline(coordinates: geodesic.coordinates, count: UInt(geodesic.coordinates.count))

    DispatchQueue.main.async(execute: {
        // Unowned reference to self to prevent retain cycle
        [unowned self] in

        self.mapboxView!.add(line)
    })


}

利用 MKGeodesicPolyline,上面的代码仅适用于短距离!
如果给定的位置远离美国到亚洲,它会在曲线上方创建一条不需要的 horizon 奇怪的线。

There is no issue on Apple Map!

请任何人帮助。 谢谢

注:iOS11.1Xcode9.1Swift4,Mapbox-iOS-SDK版本3.7.0

这是因为这条线穿过国际日期变更线,所以它一直在绕行。

为避免这种情况,您可以在经度上加上或减去 360 度来修复它。 https://github.com/mapbox/mapbox.js/issues/360

例如:

import Mapbox
import MapKit

public extension MGLPolyline {
    class func geodesicPolyline(fromCoordinate: CLLocationCoordinate2D, toCoordinate: CLLocationCoordinate2D) -> MGLPolyline {
        var coordinates = [fromCoordinate, toCoordinate]
        let geodesicPolyline = MKGeodesicPolyline(coordinates: &coordinates, count: 2)

        var normalizedCoordinates: [CLLocationCoordinate2D] = []
        var previousCoordinate: CLLocationCoordinate2D?
        for coordinate in geodesicPolyline.coordinates {
            var normalizedCoordinate = coordinate
            if let previousCoordinate = previousCoordinate, abs(previousCoordinate.longitude - coordinate.longitude) > 180 {
                if (previousCoordinate.longitude > coordinate.longitude) {
                    normalizedCoordinate.longitude += 360
                } else {
                    normalizedCoordinate.longitude -= 360
                }
            }
            normalizedCoordinates.append(normalizedCoordinate)
            previousCoordinate = normalizedCoordinate
        }

        return MGLPolyline(coordinates: normalizedCoordinates, count: UInt(geodesicPolyline.pointCount))
    }
}

public extension MKPolyline {
    var coordinates: [CLLocationCoordinate2D] {
        var coords = [CLLocationCoordinate2D](repeating: kCLLocationCoordinate2DInvalid, count: self.pointCount)
        self.getCoordinates(&coords, range: NSRange(location: 0, length: self.pointCount))
        return coords
    }
}