【发布时间】:2019-02-18 16:11:40
【问题描述】:
【问题讨论】:
标签: ios swift google-maps-sdk-ios
【问题讨论】:
标签: ios swift google-maps-sdk-ios
最简单的方法是使用GMSPolyline。
假设您有一个由CLLocationCoordinate2D 组成的coordinates 数组,并且它们的顺序正确。
let path = GMSMutablePath()
for coord in coordinates {
path.add(coord)
}
let line = GMSPolyline(path: path)
line.strokeColor = UIColor.blue
line.strokeWidth = 3.0
line.map = self.map
【讨论】:
用坐标制作路径:
extension GMSMutablePath {
convenience init(coordinates: [CLLocationCoordinate2D]) {
self.init()
for coordinate in coordinates {
add(coordinate)
}
}
}
添加地图路径:
extension GMSMapView {
func addPath(_ path: GMSPath, strokeColor: UIColor? = nil, strokeWidth: CGFloat? = nil, geodesic: Bool? = nil, spans: [GMSStyleSpan]? = nil) {
let line = GMSPolyline(path: path)
line.strokeColor = strokeColor ?? line.strokeColor
line.strokeWidth = strokeWidth ?? line.strokeWidth
line.geodesic = geodesic ?? line.geodesic
line.spans = spans ?? line.spans
line.map = self
}
}
用法:
let path = GMSMutablePath(coordinates: [<#Coordinates#>])
mapView.addPath(path)
【讨论】: