如何在Swift中的Google地图上绘制折线弧?

时间:2019-04-09 13:58:29

标签: swift core-location google-maps-sdk-ios

如何像iOS中那样在Google Maps中的两个坐标点之间绘制圆弧?

这是我尝试过的:

func createArc(
    startLocation: CLLocationCoordinate2D,
    endLocation: CLLocationCoordinate2D) -> GMSPolyline {

    let k: Double = 0.2
    let path = GMSMutablePath()
    let d = GMSGeometryDistance(startLocation, endLocation)
    let h = GMSGeometryHeading(startLocation, endLocation)

    let p = GMSGeometryOffset(startLocation, d * 0.5, h)
    let x = (1 - k * k) * d * 0.5 / (2 * k)
    let r = (1 + k * k) * d * 0.5 / (2 * k)
    let c = GMSGeometryOffset(p, x, h + 90.0)
    var h1 =  GMSGeometryHeading(c, startLocation)
    var h2 = GMSGeometryHeading(c, endLocation)

    if (h1 > 180) {
      h1 = h1 - 360
    }

    if (h2 > 180){
      h2 = h2 - 360
    }

    let numpoints = 100.0
    let step = ((h2 - h1) / Double(numpoints))
    for i in stride(from: 0.0, to: numpoints, by: 1) {
      let pi = GMSGeometryOffset(c, r, h1 + i * step)
      path.add(pi)
    }

    let polyline = GMSPolyline(path: path)
    polyline.strokeWidth = 3.0
    polyline.spans = GMSStyleSpans(
      polyline.path!,
      [GMSStrokeStyle.solidColor(UIColor(hex: "#2962ff"))],
      [20, 20], .rhumb
    )
    return polyline
  }

在一些位置,它正在创建一个圆圈。它应该在两点之间画一条弧。

我该如何实施?还有其他选择吗?

0 个答案:

没有答案