问题描述
iOS中的GSMapView具有不同的功能,可将折线覆盖在Google地图上。有没有办法在折线的起点和终点显示标记?折线的点只不过是字符串。
// MARK: - Polyline
struct Polyline: Codable {
let points: String?
}
是否可以提取第一个点和最后一个点并将其添加为Google Map上的标记?
解决方法
您如何定义折线的路径?
您可以将折线的路径定义为GMSMutablePath()
,可以在其中添加坐标值以定义折线的起点和终点。
例如,您可以定义从Melborne到Perth的折线:
let path = GMSMutablePath()
path.addLatitude(-37.81319,longitude: 144.96298)
path.addLatitude(-31.95285,longitude: 115.85734)
let polyline = GMSPolyline(path: path)
polyline.strokeWidth = 5.0
polyline.geodesic = true
polyline.map = mapView
然后,您可以使用这些坐标在折线的起点和终点精确地添加标记:
let melbourneMarker = GMSMarker();
melbourneMarker.position = CLLocationCoordinate2D(latitude: -37.81319,longitude: 144.96298)
melbourneMarker.map = mapView;
let perthMarker = GMSMarker();
perthMarker.position = CLLocationCoordinate2D(latitude: -31.95285,longitude: 115.85734)
perthMarker.map = mapView;
作为参考,您可以选中此polyline documentation。结果,它看起来像这样: