通过GMSPolyline绘制一条曲线google maps ios sdk

问题描述 投票:4回答:1

我想在两个标记之间绘制一条曲线(类似Uber),但是没有方法可以做到。有什么主意吗?

ios swift google-maps path polyline
1个回答
0
投票

我使用Bezier二次方程式绘制曲线。 You can have a look on to the implementation。这是示例代码。

func bezierPath(from startLocation: CLLocationCoordinate2D, to endLocation: CLLocationCoordinate2D) -> GMSMutablePath{

        let distance = GMSGeometryDistance(startLocation, endLocation)
        let midPoint = GMSGeometryInterpolate(startLocation, endLocation, 0.5)

        let midToStartLocHeading = GMSGeometryHeading(midPoint, startLocation)


        let controlPointAngle = 360.0 - (90.0 - midToStartLocHeading)
        let controlPoint = GMSGeometryOffset(midPoint, distance / 2.0 , controlPointAngle)

        let path = GMSMutablePath()

        let stepper = 0.05
        let range = stride(from: 0.0, through: 1.0, by: stepper)// t = [0,1]

        func calucaluatePoint(when t: Double) -> CLLocationCoordinate2D{
            let t1 = (1.0 - t)
            let latitude = t1 * t1 * startLocation.latitude + 2 * t1 * t * controlPoint.latitude + t * t * endLocation.latitude
            let longitude = t1 * t1 * startLocation.longitude + 2 * t1 * t * controlPoint.longitude + t * t * endLocation.longitude
            let point = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
            return point
        }

        range.compactMap{ calucaluatePoint(when: $0) }.forEach{path.add($0)}
        return path
    }
© www.soinside.com 2019 - 2024. All rights reserved.