首页 > 解决方案 > 在 GMSMapView 上显示折线的开始和结束标记

问题描述

iOS 中的 GSMapView 具有不同的功能来覆盖谷歌地图上的折线。有没有办法在折线的起点和终点显示标记?折线的点只是字符串。

// MARK: - Polyline
struct Polyline: Codable {
    let points: String?
}

是否可以提取第一个和最后一个点并将它们添加为谷歌地图上的标记?

标签: iosswiftgoogle-mapsgoogle-maps-markersgoogle-polyline

解决方案


您如何定义折线的路径?

您可以将折线的路径定义为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;

供您参考,您可以查看此折线文档。结果,它看起来像这样:

在此处输入图像描述


推荐阅读