【问题标题】:How do I show a polyline that only connects 2 points?如何显示仅连接 2 个点的折线?
【发布时间】:2018-12-01 11:21:19
【问题描述】:

我正在使用 iOS Google Maps 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

它显示了矩形:

由于我绘制直线的代码就在绘制矩形的代码之上,所以我的代码也应该被执行。它应该已经绘制了一条与矩形相接触的线,从北极延伸到南极。但是为什么没有这条线呢?

【问题讨论】:

  • @wings 我认为这没有多大帮助,因为该帖子的 OP 似乎正在绘制一条多段的折线。如您所见,我成功地绘制了一个矩形。我只是不知道如何画一条只有两点的折线。
  • 你真正想要的是在点之间绘制折线而不是显示矩形?
  • @wings 是的,我只想要一条连接北极和南极的测地线,经过一定的经度。我画了这个矩形只是为了测试谷歌地图本身是否工作。
  • 那么您可以使用该答案它只会在您的两点之间绘制折线它之所以有效是因为我是 OP 或者我可以向您展示我的代码

标签: ios swift google-maps google-polyline


【解决方案1】:

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

点 (90, X) 和 (-90, X) 无论 X 的值如何,都是北极和南极,并且有无限的线连接它们。我建议在其他两点之间添加赤道上的点 (0, X)。

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

我无法解释。似乎是一个错误。尝试极点附近的值,例如 89 和 -89 或 89.999 和 -89.999。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 2014-10-16
    相关资源
    最近更新 更多