Search code examples
iosswiftgoogle-mapspolylinegoogle-polyline

Swift 3 draw a polyline with Google Maps during run


I have found the below for drawing a path during a run/walk with Apple Maps

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
  }
}

However I am trying to do it with Google Maps and can't find the solution. Lots and lots of answers are for Apple Maps, but not much on Google Maps.


Solution

  • Try this

    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