Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import CoreLocation
- import MapKit
- class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
- @IBOutlet weak var mapView: MKMapView!
- let locationManager = CLLocationManager()
- override func viewDidLoad() {
- super.viewDidLoad()
- // setup locationManager
- locationManager.delegate = self
- locationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters
- locationManager.desiredAccuracy = kCLLocationAccuracyBest
- // setup mapView
- mapView.delegate = self
- mapView.showsUserLocation = true
- mapView.userTrackingMode = .follow
- // setup test data
- setupData()
- }
- override func viewDidAppear(_ animated: Bool) {
- super.viewDidAppear(animated)
- // status is not determined
- if CLLocationManager.authorizationStatus() == .notDetermined {
- locationManager.requestAlwaysAuthorization()
- }
- // authorization were denied
- else if CLLocationManager.authorizationStatus() == .denied {
- showAlert(title: "Location services were previously denied. Please enable location services for this app in Settings.")
- }
- // we do have authorization
- else if CLLocationManager.authorizationStatus() == .authorizedAlways {
- locationManager.startUpdatingLocation()
- }
- }
- func setupData() {
- // check if can monitor regions
- if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) {
- // region data
- let title = "Weber"
- let coordinate = CLLocationCoordinate2DMake(37.703026, -121.759735)
- let regionRadius = 300.0
- // setup region
- let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude), radius: regionRadius, identifier: title)
- locationManager.startMonitoring(for: region)
- // setup annotation
- let restaurantAnnotation = MKPointAnnotation()
- restaurantAnnotation.coordinate = coordinate;
- restaurantAnnotation.title = "(title)";
- mapView.addAnnotation(restaurantAnnotation)
- // setup circle
- let circle = MKCircle(center: coordinate, radius: regionRadius)
- mapView.add(circle)
- }
- else {
- print("System can't track regions")
- }
- }
- // MARK: - MKMapViewDelegate
- func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
- let circleRenderer = MKCircleRenderer(overlay: overlay)
- circleRenderer.strokeColor = UIColor.blue
- circleRenderer.lineWidth = 1.0
- return circleRenderer
- }
- // MARK: - CLLocationManagerDelegate
- func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
- showAlert(title: "enter (region.identifier)")
- }
- func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
- showAlert(title: "exit (region.identifier)")
- }
- // MARK: - Helpers
- func showAlert(title: String) {
- let alert = UIAlertController(title: title, message: nil, preferredStyle: .alert)
- alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))
- self.present(alert, animated: true, completion: nil)
- }
- }
Add Comment
Please, Sign In to add comment