Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var distance = 0.0
- @IBOutlet weak var mapView: MKMapView!
- lazy var locationManager: CLLocationManager = {
- var _locationManager = CLLocationManager()
- _locationManager.delegate = self
- _locationManager.desiredAccuracy = kCLLocationAccuracyBest
- _locationManager.activityType = .fitness
- _locationManager.distanceFilter = 10.0
- return _locationManager}()
- lazy var locations = [CLLocation]()
- extension GPSTracker: CLLocationManagerDelegate {
- func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
- for location in locations {
- let howRecent = location.timestamp.timeIntervalSinceNow
- let accuracy = location.horizontalAccuracy
- if abs(howRecent) < 10 && accuracy < 10 && accuracy > 0 && location.verticalAccuracy > 0 {
- let region = MKCoordinateRegionMakeWithDistance(location.coordinate, 500, 500)
- mapView.setRegion(region, animated: true)
- if self.locations.count > 0 {
- distance += location.distance(from: self.locations.last!)
- var coords = [CLLocationCoordinate2D]()
- coords.append(self.locations.last!.coordinate)
- coords.append(location.coordinate)
- mapView.add(MKPolyline(coordinates: &coords, count: coords.count))}
- self.locations.append(location)}}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement