在GMSMapView上显示折线的开始和结束标记

问题描述

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。结果,它看起来像这样:

enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...