Skip to content

Commit

Permalink
Add and removal now working
Browse files Browse the repository at this point in the history
  • Loading branch information
below committed Jul 29, 2022
1 parent f62f994 commit d46be22
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 30 deletions.
13 changes: 12 additions & 1 deletion GeofenceTester/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,11 @@
</view>
<connections>
<outlet property="identifierLabel" destination="YxR-jO-lcY" id="UrI-z9-atr"/>
<segue destination="cbS-Gs-Y1z" kind="unwind" identifier="unwindSegue" unwindAction="unwindActionWithUnwindSegue:" id="Or5-7R-nLn"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="USa-UM-Vqe" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
<exit id="cbS-Gs-Y1z" userLabel="Exit" sceneMemberID="exit"/>
</objects>
<point key="canvasLocation" x="-72.463768115942031" y="877.90178571428567"/>
</scene>
Expand Down Expand Up @@ -145,7 +147,16 @@
<constraint firstItem="PTq-kR-Xju" firstAttribute="leading" secondItem="Cgu-SU-7L9" secondAttribute="leading" id="ljU-vx-Efw"/>
</constraints>
</view>
<navigationItem key="navigationItem" id="1LR-FO-FdF"/>
<navigationItem key="navigationItem" id="1LR-FO-FdF">
<rightBarButtonItems>
<barButtonItem systemItem="organize" id="O8y-4g-daL"/>
<barButtonItem systemItem="add" id="BJ6-F4-XfY">
<connections>
<action selector="addRegion" destination="Mm7-Ts-hYc" id="1H1-ve-oEg"/>
</connections>
</barButtonItem>
</rightBarButtonItems>
</navigationItem>
<connections>
<outlet property="mapView" destination="PTq-kR-Xju" id="Xpb-Pa-2kJ"/>
<segue destination="abz-U4-dF9" kind="showDetail" identifier="RegionDetail" id="r59-b2-f5b"/>
Expand Down
2 changes: 1 addition & 1 deletion GeofenceTester/RegionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class RegionViewController: UIViewController {
}) {
locationManager.stopMonitoring(for: region)
}
self.dismiss(animated: true)
self.performSegue(withIdentifier: "unwindSegue", sender: self)
}

/*
Expand Down
37 changes: 9 additions & 28 deletions GeofenceTester/RegionsListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ class RegionsListViewController: UIViewController, MKMapViewDelegate {
super.viewDidLoad()

self.title = "Monitored Regions"
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add,
target: self,
action: #selector(addRegion))
registerMapAnnotationViews()

}

override func viewDidAppear(_ animated: Bool) {
Expand All @@ -35,10 +31,9 @@ class RegionsListViewController: UIViewController, MKMapViewDelegate {
private func registerMapAnnotationViews() {
mapView.register(MKMarkerAnnotationView.self,
forAnnotationViewWithReuseIdentifier: defaultReuseIdentifier)
// mapView.register(MKAnnotationView.self, forAnnotationViewWithReuseIdentifier: NSStringFromClass(SanFranciscoAnnotation.self))
}

@objc func addRegion() {
@IBAction func addRegion() {
let alertController = UIAlertController(title: "New Region", message: "Please enter a name", preferredStyle: .alert)
alertController.addTextField()
alertController.addAction(UIAlertAction(title: "Cancel",
Expand Down Expand Up @@ -93,8 +88,6 @@ class RegionsListViewController: UIViewController, MKMapViewDelegate {

for region in regions {
if let region = region as? CLCircularRegion {
// let overlay = MKCircle(center: region.center, radius: region.radius)
// mapView.addOverlay(overlay)
let annotation = MKPointAnnotation()
annotation.title = region.identifier
annotation.coordinate = region.center
Expand All @@ -104,16 +97,7 @@ class RegionsListViewController: UIViewController, MKMapViewDelegate {
mapView.addAnnotations(mapAnnotations)
mapView.showAnnotations(mapAnnotations, animated: true)
}

func coordinate(from coordinate: CLLocationCoordinate2D, latitudeDistance: CLLocationDistance, longitudeDistance: CLLocationDistance) -> CLLocationCoordinate2D {
let region = MKCoordinateRegion(center: coordinate, latitudinalMeters: latitudeDistance, longitudinalMeters: longitudeDistance)

let newLatitude = coordinate.latitude + region.span.latitudeDelta
let newLongitude = coordinate.longitude + region.span.longitudeDelta

return CLLocationCoordinate2D(latitude: newLatitude, longitude: newLongitude)
}


func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard !annotation.isKind(of: MKUserLocation.self) else {
// Make a fast exit if the annotation is the `MKUserLocation`, as it's not an annotation view we wish to customize.
Expand All @@ -122,20 +106,12 @@ class RegionsListViewController: UIViewController, MKMapViewDelegate {
let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: defaultReuseIdentifier,
for: annotation)
annotationView.canShowCallout = true
let rightButton = UIButton(type: .detailDisclosure)
let rightButton = UIButton(type: .infoLight)
annotationView.rightCalloutAccessoryView = rightButton

return annotationView
}

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKCircleRenderer(overlay: overlay)
renderer.fillColor = UIColor.blue.withAlphaComponent(0.5)
renderer.strokeColor = UIColor.blue
renderer.lineWidth = 2
return renderer
}

func mapView(_ mapView: MKMapView,
annotationView view: MKAnnotationView,
calloutAccessoryControlTapped control: UIControl){
Expand All @@ -160,4 +136,9 @@ class RegionsListViewController: UIViewController, MKMapViewDelegate {
detailView.locationManager = locationManager
}
}

@IBAction func unwindAction(unwindSegue: UIStoryboardSegue) {
self.updateMap()
}

}

0 comments on commit d46be22

Please sign in to comment.