Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. var distance = 0.0
  2. @IBOutlet weak var mapView: MKMapView!
  3.  
  4.  
  5.  
  6. lazy var locationManager: CLLocationManager = {
  7. var _locationManager = CLLocationManager()
  8. _locationManager.delegate = self
  9. _locationManager.desiredAccuracy = kCLLocationAccuracyBest
  10. _locationManager.activityType = .fitness
  11. _locationManager.distanceFilter = 10.0
  12. return _locationManager}()
  13. lazy var locations = [CLLocation]()
  14.  
  15.  
  16.  
  17. extension GPSTracker: CLLocationManagerDelegate {
  18. func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  19. for location in locations {
  20. let howRecent = location.timestamp.timeIntervalSinceNow
  21. let accuracy = location.horizontalAccuracy
  22. if abs(howRecent) < 10 && accuracy < 10 && accuracy > 0 && location.verticalAccuracy > 0 {
  23. let region = MKCoordinateRegionMakeWithDistance(location.coordinate, 500, 500)
  24. mapView.setRegion(region, animated: true)
  25. if self.locations.count > 0 {
  26. distance += location.distance(from: self.locations.last!)
  27. var coords = [CLLocationCoordinate2D]()
  28. coords.append(self.locations.last!.coordinate)
  29. coords.append(location.coordinate)
  30. mapView.add(MKPolyline(coordinates: &coords, count: coords.count))}
  31. self.locations.append(location)}}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement