Guest User

Untitled

a guest
Jan 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. import UIKit
  2. import CoreLocation
  3. import MapKit
  4.  
  5. class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
  6.  
  7. @IBOutlet weak var mapView: MKMapView!
  8.  
  9. let locationManager = CLLocationManager()
  10.  
  11. override func viewDidLoad() {
  12. super.viewDidLoad()
  13.  
  14. // setup locationManager
  15. locationManager.delegate = self
  16. locationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters
  17. locationManager.desiredAccuracy = kCLLocationAccuracyBest
  18.  
  19. // setup mapView
  20. mapView.delegate = self
  21. mapView.showsUserLocation = true
  22. mapView.userTrackingMode = .follow
  23.  
  24. // setup test data
  25. setupData()
  26. }
  27.  
  28. override func viewDidAppear(_ animated: Bool) {
  29. super.viewDidAppear(animated)
  30.  
  31. // status is not determined
  32. if CLLocationManager.authorizationStatus() == .notDetermined {
  33. locationManager.requestAlwaysAuthorization()
  34. }
  35. // authorization were denied
  36. else if CLLocationManager.authorizationStatus() == .denied {
  37. showAlert(title: "Location services were previously denied. Please enable location services for this app in Settings.")
  38. }
  39. // we do have authorization
  40. else if CLLocationManager.authorizationStatus() == .authorizedAlways {
  41. locationManager.startUpdatingLocation()
  42. }
  43. }
  44.  
  45. func setupData() {
  46. // check if can monitor regions
  47. if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) {
  48.  
  49. // region data
  50. let title = "Weber"
  51. let coordinate = CLLocationCoordinate2DMake(37.703026, -121.759735)
  52. let regionRadius = 300.0
  53.  
  54. // setup region
  55. let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude), radius: regionRadius, identifier: title)
  56. locationManager.startMonitoring(for: region)
  57.  
  58. // setup annotation
  59. let restaurantAnnotation = MKPointAnnotation()
  60. restaurantAnnotation.coordinate = coordinate;
  61. restaurantAnnotation.title = "(title)";
  62. mapView.addAnnotation(restaurantAnnotation)
  63.  
  64. // setup circle
  65. let circle = MKCircle(center: coordinate, radius: regionRadius)
  66. mapView.add(circle)
  67. }
  68. else {
  69. print("System can't track regions")
  70. }
  71. }
  72.  
  73. // MARK: - MKMapViewDelegate
  74. func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
  75. let circleRenderer = MKCircleRenderer(overlay: overlay)
  76. circleRenderer.strokeColor = UIColor.blue
  77. circleRenderer.lineWidth = 1.0
  78. return circleRenderer
  79. }
  80.  
  81. // MARK: - CLLocationManagerDelegate
  82. func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
  83. showAlert(title: "enter (region.identifier)")
  84.  
  85. }
  86.  
  87. func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
  88. showAlert(title: "exit (region.identifier)")
  89. }
  90.  
  91. // MARK: - Helpers
  92. func showAlert(title: String) {
  93. let alert = UIAlertController(title: title, message: nil, preferredStyle: .alert)
  94. alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))
  95. self.present(alert, animated: true, completion: nil)
  96. }
  97. }
Add Comment
Please, Sign In to add comment