Swift 3 在 运行 期间用 Google 地图绘制折线

Swift 3 draw a polyline with Google Maps during run

我在 run/walk 使用 Apple 地图

期间找到了绘制路径的方法
extension NewRunViewController: MKMapViewDelegate {
  func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
    if !overlay.isKindOfClass(MKPolyline) {
      return nil
    }

    let polyline = overlay as! MKPolyline
    let renderer = MKPolylineRenderer(polyline: polyline)
    renderer.strokeColor = UIColor.blueColor()
    renderer.lineWidth = 3
    return renderer
  }
}

但是我试图用 Google 地图来解决这个问题,但找不到解决方案。 很多答案都是针对 Apple 地图的,但 Google 地图上的答案并不多。

试试这个

let path = GMSMutablePath()
//Change coordinates
path.add(CLLocationCoordinate2D(latitude: -33.85, longitude: 151.20))
path.add(CLLocationCoordinate2D(latitude: -33.70, longitude: 151.40))
path.add(CLLocationCoordinate2D(latitude: -33.73, longitude: 151.41))
let polyline = GMSPolyline(path: path)
polyline.strokeColor = UIColor.blue
polyline.strokeWidth = 3.0
polyline.map = mapView

这是我的解决方案 Swift 5.

private var trackLocations: [CLLocation] = []

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    
        for location in locations {
            
            self.trackLocations.append(location)
            
            let index = self.trackLocations.count
            
            if index > 2 {
            
                let path = GMSMutablePath()
                path.add(self.trackLocations[index - 1].coordinate)
                path.add(self.trackLocations[index - 2].coordinate)
                
                let polyline = GMSPolyline(path: path)
                polyline.strokeColor = Global.iOSMapRendererColor
                polyline.strokeWidth = 5.0
                polyline.map = self.mapView
                
            }
            
        }
}