代码之家  ›  专栏  ›  技术社区  ›  Sweeper

如何显示仅连接2个点的多段线?

  •  0
  • Sweeper  · 技术社区  · 6 年前

    我使用iOS谷歌地图API来显示地图。我想在特定的经度处添加一条线,这样用户就可以知道经度在哪里。

    这就是我所尝试的:

    let path = GMSMutablePath()
    path.add(CLLocationCoordinate2D(latitude: -90, longitude: -122))
    path.add(CLLocationCoordinate2D(latitude: 90, longitude: -122))
    polyline = GMSPolyline(path: path)
    polyline.geodesic = true
    polyline.strokeColor = .red
    polyline.strokeWidth = 20 // I have set this to a big number to guarantee that my poor eyesight can see the line
    polyline.map = mapView
    

    我在任何地方都看不到这条线!

    作为一个控制实验,我复制了从 tutorial 并将其粘贴在上述代码之后:

    let rect = GMSMutablePath()
    rect.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.0))
    rect.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.0))
    rect.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.2))
    rect.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.2))
    
    // Create the polygon, and assign it to the map.
    let polygon = GMSPolygon(path: rect)
    polygon.fillColor = UIColor(red: 0.25, green: 0, blue: 0, alpha: 0.2);
    polygon.strokeColor = .black
    polygon.strokeWidth = 2
    polygon.map = mapView
    

    它显示了矩形:

    enter image description here

    因为我画线的代码就在画矩形的代码的正上方,所以我的代码也应该被执行。它应该画一条与矩形相接触的线,从北极延伸到南极。但为什么没有这样的界线呢?

    1 回复  |  直到 6 年前
        1
  •  1
  •   vacawama    6 年前

    问题是你的两点不仅仅是两个任意的点,而且它们之间没有一条最短的线。

    点(90,x)和(-90,x)是南北极,不管x的值是多少,并且有无限的线连接它们。我建议把赤道上的点(0,x)加在其他两点之间。

    我在赤道上的北极和南极之间加了一个点,现在它只画了一条到赤道的线。

    我不能解释。像个虫子。尝试极附近的值,例如 89 -89 89.999 -89.999 .

    推荐文章