Skip to content

Instantly share code, notes, and snippets.

@hesbryce
Created March 3, 2018 22:34
Show Gist options
  • Save hesbryce/5e0fe74cf5e9b534e321166a02faf7be to your computer and use it in GitHub Desktop.
Save hesbryce/5e0fe74cf5e9b534e321166a02faf7be to your computer and use it in GitHub Desktop.
FindPotHoles.swift
//
// ViewController.swift
// FindPotHoles
//
// Created by Bryce Ellis on 3/3/18.
// Copyright © 2018 Bryce Ellis. All rights reserved.
//
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var isOn = false
func createAlert (title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "YES", style: UIAlertActionStyle.default, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
print("YES")
}))
if title == "Yes" {
/*
This asks the user for their current location -
Opens Map View and Current Location is noted by a blue dot.
Wondering why I cannot zoom out
*/
let manager = CLLocationManager()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations[0]
let span: MKCoordinateSpan = MKCoordinateSpanMake(0.5, 0.5)
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region: MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
myMapView.setRegion(region, animated: true)
self.myMapView.showsUserLocation = true
} // End of Map Code
}
alert.addAction(UIAlertAction(title: "NO", style: UIAlertActionStyle.default, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
print("NO")
}))
if title == "NO" {
func segueAction(_ sender: Any)
{
performSegue(withIdentifier: "show_segue1", sender: self)
}
// performSegue(withIdentifier: "show_segue1", sender: self)
}
self.present(alert, animated: true, completion: nil)
}
override func viewDidAppear(_ animated: Bool) {
createAlert(title: "Did you find a pothole?", message: "Are you in Shelby County?")
}
@IBOutlet weak var myMapView: MKMapView!
/*
This asks the user for their current location -
Opens Map View and Current Location is noted by a blue dot.
Wondering why I cannot zoom out
*/
let manager = CLLocationManager()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations[0]
let span: MKCoordinateSpan = MKCoordinateSpanMake(0.015, 0.015)
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region: MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
myMapView.setRegion(region, animated: true)
self.myMapView.showsUserLocation = true
} // End of Map Code
//Segue Button To A Better Page.
@IBAction func segueAction(_ sender: Any)
{
performSegue(withIdentifier: "show_segue1", sender: self)
}
@objc func addAnnotation(press: UILongPressGestureRecognizer) {
if press.state == .began {
let location = press.location(in: myMapView)
let newCoordinate = myMapView.convert(location, toCoordinateFrom: self.myMapView)
let annotation = MKPointAnnotation()
annotation.coordinate = newCoordinate
annotation.title = "This is where I found the pot hole"
annotation.subtitle = "Shelby county needs to fix this"
myMapView.addAnnotation(annotation)
}
}
override func viewDidLoad() {
super.viewDidLoad()
let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(addAnnotation(press:)))
longPressGestureRecognizer.minimumPressDuration = 2.0
myMapView.addGestureRecognizer(longPressGestureRecognizer)
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment