Commit 05261150 authored by Ariba Siddiqui's avatar Ariba Siddiqui
Browse files

comments for directions and other restructuring

parent 118673de
No preview for this file type
......@@ -7,7 +7,6 @@
objects = {
/* Begin PBXBuildFile section */
09AD0D752753EF1C00E400F7 /* MapSerachView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09AD0D742753EF1C00E400F7 /* MapSerachView.swift */; };
09AD0D772753EF5000E400F7 /* LocationSearch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 09AD0D762753EF5000E400F7 /* LocationSearch.swift */; };
2517A35527502266006F2B81 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2517A35427502266006F2B81 /* AppDelegate.swift */; };
2517A35727502266006F2B81 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2517A35627502266006F2B81 /* SceneDelegate.swift */; };
......@@ -38,7 +37,6 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
09AD0D742753EF1C00E400F7 /* MapSerachView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapSerachView.swift; sourceTree = "<group>"; };
09AD0D762753EF5000E400F7 /* LocationSearch.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationSearch.swift; sourceTree = "<group>"; };
2517A35127502266006F2B81 /* Location Tutorial.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Location Tutorial.app"; sourceTree = BUILT_PRODUCTS_DIR; };
2517A35427502266006F2B81 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
......@@ -110,7 +108,6 @@
2517A35D2750226C006F2B81 /* Assets.xcassets */,
2517A35F2750226C006F2B81 /* LaunchScreen.storyboard */,
2517A3622750226C006F2B81 /* Info.plist */,
09AD0D742753EF1C00E400F7 /* MapSerachView.swift */,
09AD0D762753EF5000E400F7 /* LocationSearch.swift */,
);
path = "Location Tutorial";
......@@ -266,7 +263,6 @@
files = (
09AD0D772753EF5000E400F7 /* LocationSearch.swift in Sources */,
2517A35927502266006F2B81 /* ViewController.swift in Sources */,
09AD0D752753EF1C00E400F7 /* MapSerachView.swift in Sources */,
2517A35527502266006F2B81 /* AppDelegate.swift in Sources */,
2517A35727502266006F2B81 /* SceneDelegate.swift in Sources */,
);
......
......@@ -14,8 +14,8 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "86"
endingLineNumber = "86"
startingLineNumber = "84"
endingLineNumber = "84"
landmarkName = "goButtonTapped(_:)"
landmarkType = "7">
</BreakpointContent>
......@@ -30,8 +30,8 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "105"
endingLineNumber = "105"
startingLineNumber = "103"
endingLineNumber = "103"
landmarkName = "getDirections()"
landmarkType = "7">
</BreakpointContent>
......@@ -46,8 +46,8 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "106"
endingLineNumber = "106"
startingLineNumber = "104"
endingLineNumber = "104"
landmarkName = "getDirections()"
landmarkType = "7">
</BreakpointContent>
......@@ -62,58 +62,10 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "107"
endingLineNumber = "107"
landmarkName = "getDirections()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "298D89E0-B8E7-4739-97A7-7799C4E2704E"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "109"
endingLineNumber = "109"
startingLineNumber = "105"
endingLineNumber = "105"
landmarkName = "getDirections()"
landmarkType = "7">
<Locations>
<Location
uuid = "298D89E0-B8E7-4739-97A7-7799C4E2704E - 5a8fd180874c388"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "Location_Tutorial.ViewController.getDirections() -&gt; ()"
moduleName = "Location Tutorial"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/aribasiddiqui/Downloads/Location%20Tutorial/Location%20Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "100"
endingLineNumber = "100"
offsetFromSymbolStart = "428">
</Location>
<Location
uuid = "298D89E0-B8E7-4739-97A7-7799C4E2704E - 53aed4c77a2b419f"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "closure #1 (Swift.Optional&lt;__C.MKDirectionsResponse&gt;, Swift.Optional&lt;Swift.Error&gt;) -&gt; () in Location_Tutorial.ViewController.getDirections() -&gt; ()"
moduleName = "Location Tutorial"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/aribasiddiqui/Downloads/Location%20Tutorial/Location%20Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "100"
endingLineNumber = "100"
offsetFromSymbolStart = "108">
</Location>
</Locations>
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
......@@ -126,8 +78,8 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "111"
endingLineNumber = "111"
startingLineNumber = "113"
endingLineNumber = "113"
landmarkName = "getDirections()"
landmarkType = "7">
</BreakpointContent>
......@@ -142,8 +94,8 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "113"
endingLineNumber = "113"
startingLineNumber = "119"
endingLineNumber = "119"
landmarkName = "getDirections()"
landmarkType = "7">
</BreakpointContent>
......@@ -158,8 +110,8 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "114"
endingLineNumber = "114"
startingLineNumber = "120"
endingLineNumber = "120"
landmarkName = "getDirections()"
landmarkType = "7">
</BreakpointContent>
......@@ -174,56 +126,24 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "119"
endingLineNumber = "119"
landmarkName = "getDirections()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "09F7531A-1930-428C-96D6-F706EE65214C"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "125"
endingLineNumber = "125"
landmarkName = "createDirectionsRequest(from:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "2160C47A-F8E5-46F3-9409-EDE5414885DD"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "126"
endingLineNumber = "126"
landmarkName = "createDirectionsRequest(from:)"
landmarkName = "getDirections()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "C3FEF63B-F36D-41E8-80DF-D4F88C907C81"
uuid = "2160C47A-F8E5-46F3-9409-EDE5414885DD"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "128"
endingLineNumber = "128"
startingLineNumber = "134"
endingLineNumber = "134"
landmarkName = "createDirectionsRequest(from:)"
landmarkType = "7">
</BreakpointContent>
......@@ -238,8 +158,8 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "127"
endingLineNumber = "127"
startingLineNumber = "135"
endingLineNumber = "135"
landmarkName = "createDirectionsRequest(from:)"
landmarkType = "7">
</BreakpointContent>
......@@ -254,28 +174,12 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "129"
endingLineNumber = "129"
startingLineNumber = "136"
endingLineNumber = "136"
landmarkName = "createDirectionsRequest(from:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "62E4DD88-9016-4FF0-9C32-5FBF2E22A604"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "140"
endingLineNumber = "140"
landmarkName = "resetMapView(withNew:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
......@@ -286,8 +190,8 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "110"
endingLineNumber = "110"
startingLineNumber = "108"
endingLineNumber = "108"
landmarkName = "getDirections()"
landmarkType = "7">
</BreakpointContent>
......@@ -302,8 +206,8 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "165"
endingLineNumber = "165"
startingLineNumber = "172"
endingLineNumber = "172"
landmarkName = "mapView(_:rendererFor:)"
landmarkType = "7">
</BreakpointContent>
......@@ -318,8 +222,8 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "164"
endingLineNumber = "164"
startingLineNumber = "171"
endingLineNumber = "171"
landmarkName = "mapView(_:rendererFor:)"
landmarkType = "7">
</BreakpointContent>
......
......@@ -16,7 +16,7 @@
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<mapView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" mapType="standard" showsUserLocation="YES" translatesAutoresizingMaskIntoConstraints="NO" id="eRy-hV-Pvo">
<mapView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" mapType="standard" showsUserLocation="YES" showsCompass="NO" translatesAutoresizingMaskIntoConstraints="NO" id="eRy-hV-Pvo">
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
</mapView>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="ac8-fP-bc3">
......
......@@ -2,8 +2,6 @@
// LocationFinder.swift
// MapKit Testing Search Functionality
//
// Created by Brian Porumb on 25.11.21.
//
import Foundation
import Combine
......@@ -11,7 +9,7 @@ import MapKit
class LocationService: NSObject, ObservableObject {
// Differnt search states
// Different search states
enum LocationStatus: Equatable {
case idle
case noResults
......
//
// ContentView.swift
// MapSearchView.swift
// MapKit Testing Search Functionality
//
// Created by Brian Porumb on 25.11.21.
//
import SwiftUI
......
//
// ViewController.swift
// Location Tutorial
//
// Created by Huang on 2021/11/25.
// MapKit Routing, Annotations and Location
//
import UIKit
......@@ -14,26 +12,31 @@ class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
var regionInMeters: Double = 10000 //scale of the map
// ### code for directions start ###
var directionsArray: [MKDirections] = []
// ### code for directions end ###
override func viewDidLoad() {
super.viewDidLoad()
checkLocationServices()
setupMapView()
}
func setupMapView(){
mapView.delegate=self
}
func setupLocationManager() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
}
// helper function to display alerts to user
func displayAlert(alertTitle: String, alertMessage: String) {
let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true)
}
func centerViewOnUserLocation() {
if let location = locationManager.location?.coordinate {
let region = MKCoordinateRegion.init(center: location, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
......@@ -41,18 +44,18 @@ class ViewController: UIViewController {
}
}
// check if user's location services are enabled
func checkLocationServices() {
if CLLocationManager.locationServicesEnabled() {
setupLocationManager()
checkLocationAuthorization()
} else {
// Show alert letting the user know they have to turn this on.
print("Location Services are not enabled")
displayAlert(alertTitle: "Location Services are not turned on", alertMessage: "You must enable your Location Services to perform this action")
}
}
// checking authorization status
func checkLocationAuthorization() {
let manager = CLLocationManager()
switch manager.authorizationStatus {
......@@ -76,29 +79,24 @@ class ViewController: UIViewController {
}
}
// ### code for directions start ###
func setupMapView(){
mapView.delegate=self
}
// action triggered on the "GO" button tap
@IBAction func goButtonTapped(_ sender: Any) {
print("GO button tapped")
getDirections()
}
func getLocationCoordinates() -> CLLocationCoordinate2D {
let cologne = MKPointAnnotation()
cologne.title = "Cologne"
cologne.coordinate = CLLocationCoordinate2D(latitude: 50.9375, longitude: 6.9603)
mapView.addAnnotation(cologne)
return cologne.coordinate //hard coded coordinates for Cologne
//to prevent throttling and multiple renders
func resetMapView(withNew directions: MKDirections) {
mapView.removeOverlays(mapView.overlays)
directionsArray.append(directions)
let _ = directionsArray.map { $0.cancel() }
}
// main function for routing
func getDirections() {
guard let location = locationManager.location?.coordinate else {
//TODO: Inform user we don't have their current location
print("Location not found")
displayAlert(alertTitle: "Location not found", alertMessage: "App cannot access your location")
return
}
......@@ -107,12 +105,21 @@ class ViewController: UIViewController {
resetMapView(withNew: directions)
directions.calculate { [unowned self] (response, error) in
//TODO: Handle error if needed
guard let response = response else { return } //TODO: Show response not available in an alert
if error != nil {
print("Directions couldn't be computed")
displayAlert(alertTitle: "Directions couldn't be computed", alertMessage: "App cannot find directions to the destination")
return
}
guard let response = response else {
print("Response not found")
displayAlert(alertTitle: "Directions couldn't be computed", alertMessage: "App cannot find directions to the destination")
return
}
print("total routes found: \(response.routes.count)")
for route in response.routes {
self.mapView.addOverlay(route.polyline, level: .aboveRoads)
var directionsRegion: MKCoordinateRegion = MKCoordinateRegion.init(route.polyline.boundingMapRect)
self.mapView.addOverlay(route.polyline, level: .aboveRoads) //add an overlay for each route
//polyline: connected line segments that don't form a loop
var directionsRegion: MKCoordinateRegion = MKCoordinateRegion.init(route.polyline.boundingMapRect) // recenter
//to increase the span of the region in order to add some margin
directionsRegion.span.latitudeDelta *= 1.2
directionsRegion.span.longitudeDelta *= 1.2
......@@ -121,31 +128,30 @@ class ViewController: UIViewController {
}
}
// creates a new request by configuring different properties
func createDirectionsRequest(from coordinate: CLLocationCoordinate2D) -> MKDirections.Request {
let destinationCoordinate = getLocationCoordinates()
let startingLocation = MKPlacemark(coordinate: coordinate) //user's location
let destination = MKPlacemark(coordinate: destinationCoordinate)
let request = MKDirections.Request()
request.source = MKMapItem(placemark: startingLocation)
request.destination = MKMapItem(placemark: destination)
request.transportType = .automobile
request.requestsAlternateRoutes = true
return request
}
//to prevent throttling and multiple renders
func resetMapView(withNew directions: MKDirections) {
mapView.removeOverlays(mapView.overlays)
directionsArray.append(directions)
let _ = directionsArray.map { $0.cancel() }
// hard-coded to return coordinates of Southampton
func getLocationCoordinates() -> CLLocationCoordinate2D {
let cologne = MKPointAnnotation()
cologne.title = "Southampton"
cologne.coordinate = CLLocationCoordinate2D(latitude: 50.9097, longitude: -1.4044)
mapView.addAnnotation(cologne) // add annotation(a pin) for Southampton
return cologne.coordinate
}
// ### code for directions end ###
}
extension ViewController: CLLocationManagerDelegate, MKMapViewDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
......@@ -158,7 +164,8 @@ extension ViewController: CLLocationManagerDelegate, MKMapViewDelegate {
checkLocationAuthorization()
}
// ### code for directions start ###
// called each time the bounding map rectangle of an overlay intersects the visible region of a map
// defines 'how' the overlay should look like -> renderer properties are specified here
func mapView(_ mapView: MKMapView,
rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(overlay: overlay)
......@@ -168,22 +175,22 @@ extension ViewController: CLLocationManagerDelegate, MKMapViewDelegate {
return renderer
}
// Delegate method for annotations
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard annotation is MKPointAnnotation else { return nil }
let identifier = "Annotation"
// dequeue an existing annotation view before creating a new one as new annotations move on-screen
// (performance enhancement)
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView!.canShowCallout = true
} else {
annotationView!.annotation = annotation
}
return annotationView
}
// ### code for directions end ###
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment