Commit 1377bbc9 authored by Ariba Siddiqui's avatar Ariba Siddiqui
Browse files

Revert "comments for directions and other restructuring"

This reverts commit 05261150
parent 05261150
No preview for this file type
......@@ -7,6 +7,7 @@
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 */; };
......@@ -37,6 +38,7 @@
/* 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>"; };
......@@ -108,6 +110,7 @@
2517A35D2750226C006F2B81 /* Assets.xcassets */,
2517A35F2750226C006F2B81 /* LaunchScreen.storyboard */,
2517A3622750226C006F2B81 /* Info.plist */,
09AD0D742753EF1C00E400F7 /* MapSerachView.swift */,
09AD0D762753EF5000E400F7 /* LocationSearch.swift */,
);
path = "Location Tutorial";
......@@ -263,6 +266,7 @@
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 = "84"
endingLineNumber = "84"
startingLineNumber = "86"
endingLineNumber = "86"
landmarkName = "goButtonTapped(_:)"
landmarkType = "7">
</BreakpointContent>
......@@ -30,8 +30,8 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "103"
endingLineNumber = "103"
startingLineNumber = "105"
endingLineNumber = "105"
landmarkName = "getDirections()"
landmarkType = "7">
</BreakpointContent>
......@@ -46,8 +46,8 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "104"
endingLineNumber = "104"
startingLineNumber = "106"
endingLineNumber = "106"
landmarkName = "getDirections()"
landmarkType = "7">
</BreakpointContent>
......@@ -62,10 +62,58 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "105"
endingLineNumber = "105"
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"
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
......@@ -78,8 +126,8 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "113"
endingLineNumber = "113"
startingLineNumber = "111"
endingLineNumber = "111"
landmarkName = "getDirections()"
landmarkType = "7">
</BreakpointContent>
......@@ -94,8 +142,8 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "119"
endingLineNumber = "119"
startingLineNumber = "113"
endingLineNumber = "113"
landmarkName = "getDirections()"
landmarkType = "7">
</BreakpointContent>
......@@ -110,8 +158,8 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "120"
endingLineNumber = "120"
startingLineNumber = "114"
endingLineNumber = "114"
landmarkName = "getDirections()"
landmarkType = "7">
</BreakpointContent>
......@@ -126,12 +174,28 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "126"
endingLineNumber = "126"
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
......@@ -142,8 +206,24 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "134"
endingLineNumber = "134"
startingLineNumber = "126"
endingLineNumber = "126"
landmarkName = "createDirectionsRequest(from:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "C3FEF63B-F36D-41E8-80DF-D4F88C907C81"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "128"
endingLineNumber = "128"
landmarkName = "createDirectionsRequest(from:)"
landmarkType = "7">
</BreakpointContent>
......@@ -158,8 +238,8 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "135"
endingLineNumber = "135"
startingLineNumber = "127"
endingLineNumber = "127"
landmarkName = "createDirectionsRequest(from:)"
landmarkType = "7">
</BreakpointContent>
......@@ -174,12 +254,28 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "136"
endingLineNumber = "136"
startingLineNumber = "129"
endingLineNumber = "129"
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
......@@ -190,8 +286,8 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "108"
endingLineNumber = "108"
startingLineNumber = "110"
endingLineNumber = "110"
landmarkName = "getDirections()"
landmarkType = "7">
</BreakpointContent>
......@@ -206,8 +302,8 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "172"
endingLineNumber = "172"
startingLineNumber = "165"
endingLineNumber = "165"
landmarkName = "mapView(_:rendererFor:)"
landmarkType = "7">
</BreakpointContent>
......@@ -222,8 +318,8 @@
filePath = "Location Tutorial/ViewController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "171"
endingLineNumber = "171"
startingLineNumber = "164"
endingLineNumber = "164"
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" showsCompass="NO" translatesAutoresizingMaskIntoConstraints="NO" id="eRy-hV-Pvo">
<mapView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" mapType="standard" showsUserLocation="YES" 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,6 +2,8 @@
// LocationFinder.swift
// MapKit Testing Search Functionality
//
// Created by Brian Porumb on 25.11.21.
//
import Foundation
import Combine
......@@ -9,7 +11,7 @@ import MapKit
class LocationService: NSObject, ObservableObject {
// Different search states
// Differnt search states
enum LocationStatus: Equatable {
case idle
case noResults
......
//
// MapSearchView.swift
// ContentView.swift
// MapKit Testing Search Functionality
//
// Created by Brian Porumb on 25.11.21.
//
import SwiftUI
......
//
// ViewController.swift
// MapKit Routing, Annotations and Location
// Location Tutorial
//
// Created by Huang on 2021/11/25.
//
import UIKit
......@@ -12,31 +14,26 @@ 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)
......@@ -44,18 +41,18 @@ class ViewController: UIViewController {
}
}
// check if user's location services are enabled
func checkLocationServices() {
if CLLocationManager.locationServicesEnabled() {
setupLocationManager()
checkLocationAuthorization()
} else {
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")
// Show alert letting the user know they have to turn this on.
}
}
// checking authorization status
func checkLocationAuthorization() {
let manager = CLLocationManager()
switch manager.authorizationStatus {
......@@ -79,24 +76,29 @@ class ViewController: UIViewController {
}
}
// action triggered on the "GO" button tap
// ### code for directions start ###
func setupMapView(){
mapView.delegate=self
}
@IBAction func goButtonTapped(_ sender: Any) {
print("GO button tapped")
getDirections()
}
//to prevent throttling and multiple renders
func resetMapView(withNew directions: MKDirections) {
mapView.removeOverlays(mapView.overlays)
directionsArray.append(directions)
let _ = directionsArray.map { $0.cancel() }
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
}
// main function for routing
func getDirections() {
guard let location = locationManager.location?.coordinate else {
print("Location not found")
displayAlert(alertTitle: "Location not found", alertMessage: "App cannot access your location")
//TODO: Inform user we don't have their current location
return
}
......@@ -105,21 +107,12 @@ class ViewController: UIViewController {
resetMapView(withNew: directions)
directions.calculate { [unowned self] (response, error) in
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
}
//TODO: Handle error if needed
guard let response = response else { return } //TODO: Show response not available in an alert
print("total routes found: \(response.routes.count)")
for route in response.routes {
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
self.mapView.addOverlay(route.polyline, level: .aboveRoads)
var directionsRegion: MKCoordinateRegion = MKCoordinateRegion.init(route.polyline.boundingMapRect)
//to increase the span of the region in order to add some margin
directionsRegion.span.latitudeDelta *= 1.2
directionsRegion.span.longitudeDelta *= 1.2
......@@ -128,30 +121,31 @@ 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
}
// 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
//to prevent throttling and multiple renders
func resetMapView(withNew directions: MKDirections) {
mapView.removeOverlays(mapView.overlays)
directionsArray.append(directions)
let _ = directionsArray.map { $0.cancel() }
}
// ### code for directions end ###
}
extension ViewController: CLLocationManagerDelegate, MKMapViewDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
......@@ -164,8 +158,7 @@ extension ViewController: CLLocationManagerDelegate, MKMapViewDelegate {
checkLocationAuthorization()
}
// 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
// ### code for directions start ###
func mapView(_ mapView: MKMapView,
rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(overlay: overlay)
......@@ -175,22 +168,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