How to draw an arc on Google Maps in iOS?

后端 未结 5 477
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-11 07:26

How to draw an arc between two coordinate points in Google Maps like in this image and same like facebook post in iOS ?

5条回答
  •  春和景丽
    2020-12-11 08:08

    I used Bezier quadratic equation to draw curved lines. You can have a look on to the implementation. Here is the sample code.

    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 calculatePoint(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.map { calculatePoint(when: $0) }.forEach { path.add($0) }
            return path
     }
    

提交回复
热议问题