Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Swift by asdasdasd ( 7 years ago )
var locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.allowsBackgroundLocationUpdates = true
locationManager.startUpdatingLocation()

var locations = [CLLocation]()

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        
     guard let location = manager.location else { return }
     locations.append(location)

     let coordinate = location.coordinate
        
     let region = MKCoordinateRegion(center: coordinate, span: .init(latitudeDelta: 0.005, longitudeDelta: 0.005))
     mapView.setRegion(region, animated: true)
            
    if locations.count > 1 {
        let sourceIndex = locations.count - 1
        let destinationIndex = locations.count - 2
                
        let coords1 = locations[sourceIndex].coordinate
        let coords2 = locations[destinationIndex].coordinate
                
        var a = [coords1, coords2]
        let polyline = MKGeodesicPolyline(coordinates: &a, count: a.count)
        mapView.addOverlay(polyline)
    }
}

 

Revise this Paste

Your Name: Code Language: