Swift 3 draw a polyline with Google Maps during run

3.5k views Asked by At

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.

2

There are 2 answers

0
RajeshKumar R On BEST ANSWER

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
0
mis On

This is my solution with 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
                
            }
            
        }
}