【发布时间】: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