Advice - Swift - User Check-in inside MK-circle

Hello,

I’m learning swift now, so this is my first question here :smiley: I’m creating the app with some annotations on the map. I’ve added MKCircles in radius 200m from the annotation. This should be a radius in which user can check-in and save his loacation. Can I use distance(from:)to do it?

In general I’m looking for some tips/advices how to set this check in (Could be check-in button inside the annotation view) that saves user location if user is in radius 200m from the annotation. The check-in should be connected to the name and describtion of the annotation (sczyt.name and szczyt.opis), and the annotation name should be saved in a table view “Zdobyte Szczyty”, which is another tab bar view in the app. My mainstoryboard looks like this:
Screenshot 2021-06-11 at 13.52.04
There is a Map controller and Zdobyte Szczyty controller. so I would like to save location only if in radius 200m from annotation as annotation name (szczyt.name) and describtion (szczyt.opis) in table view, which is based in Zdobyte Szczyty viewcontroller.
Here is my map viewcontroller.swift:

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate{

 let locationManager = CLLocationManager()

struct Szczyt {

  let name: String
  let opis: String
  let lattitude: CLLocationDegrees
  let longtitude: CLLocationDegrees

    var coordinate: CLLocationCoordinate2D {

        .init(latitude: lattitude, longitude: longtitude)

    }
 }


@IBOutlet weak var mapView: MKMapView!

@IBAction func mapTypeSegmentSelected(_ sender: UISegmentedControl) {

        switch sender.selectedSegmentIndex {

        case 0:

            mapView.mapType = .standard

        case 1:

            mapView.mapType = .satellite

        default:

            mapView.mapType = .hybrid

        }
     }

private(set) var circles: [MKCircle]!

let szczyty = [Szczyt(name: “one”, opis: “describtion”, lattitude: 50.825061725039226, longtitude: 16.908595782487315),

               Szczyt(name: “two, opis: “describtion”, lattitude: 50.223874478583854, longtitude: 20.996341184611302),

               Szczyt(name: “three”, opis: “describtion”, lattitude: 50.756134079897516, longtitude: 15.984675411850157)]


override func viewDidLoad() {

    super.viewDidLoad()

    checkLocationServices()

    znajdzSzczytyNaMapie(szczyty)

    circles = szczyty.map {

           MKCircle(center: $0.coordinate, radius: 200)
    }

    mapView.addOverlays(circles!)

    mapView.delegate = self
    }
 
    func checkLocationServices() {

      if CLLocationManager.locationServicesEnabled() {

        checkLocationAuthorization()

      } else {

        // Show alert letting the user know they have to turn this on.      
        }
     }

    func checkLocationAuthorization() {

      switch CLLocationManager.authorizationStatus() {

      case .authorizedWhenInUse:

        mapView.showsUserLocation = true

       case .denied: // Show alert telling users how to turn on permissions

       break

      case .notDetermined:

        locationManager.requestWhenInUseAuthorization()

        mapView.showsUserLocation = true

      case .restricted: // Show an alert letting them know what’s up

       break

      case .authorizedAlways:

       break
       }
  }


func znajdzSzczytyNaMapie(_ szczyty: [Szczyt]) {

  for szczyt in szczyty {

    let annotations = MKPointAnnotation()

    annotations.title = szczyt.name

    annotations.subtitle = szczyt.opis

    annotations.coordinate = CLLocationCoordinate2D(latitude:

      szczyt.lattitude, longitude: szczyt.longtitude)

    mapView.addAnnotation(annotations)
  }
}


 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    guard !(annotation is MKUserLocation) else { return nil }

    let annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "MyMarker")

    switch annotation.title!! {

        case “one”:

            annotationView.markerTintColor = UIColor(red: 0.86, green: 0.99, blue: 0.79, alpha: 1.00)

            annotationView.glyphImage = UIImage(named: "bald")

        case “two”:

            annotationView.markerTintColor = UIColor(red: 0.80, green: 0.98, blue: 0.73, alpha: 1.00)

            annotationView.glyphImage = UIImage(named: "bear")

        case “three”:

            annotationView.markerTintColor = UIColor(red: 0.73, green: 0.98, blue: 0.68, alpha: 1.00)

            annotationView.glyphImage = UIImage(named: "spruces")
    }

    return annotationView
 }

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {

    let circleRenderer = MKCircleRenderer(overlay: overlay)

    circleRenderer.strokeColor = UIColor.green

    circleRenderer.fillColor = UIColor.green

    circleRenderer.alpha = 0.3

    circleRenderer.lineWidth = 1.0

    return circleRenderer
 }

Here is my ZdobyteSzczyty view controller:

import UIKit

class Zdobyte_ViewController: UIViewController {
 
    override func viewDidLoad() { 
       super.viewDidLoad()

    }
}

This topic was automatically closed after 166 days. New replies are no longer allowed.