FULL OF BUGS

This commit is contained in:
Max Hunt 2019-05-29 18:02:43 +01:00
parent 0f4edf7f3f
commit c924c5cd39
2 changed files with 237 additions and 5 deletions

View File

@ -200,10 +200,15 @@
<rect key="frame" x="20" y="179" width="374" height="75"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" image="searchBar" translatesAutoresizingMaskIntoConstraints="NO" id="VD5-7k-Htb" userLabel="searchBarImg">
<button opaque="NO" userInteractionEnabled="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="3Xt-Rr-hs1" userLabel="searchBarBg">
<rect key="frame" x="0.0" y="8" width="374" height="59"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</imageView>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<color key="tintColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<state key="normal" backgroundImage="searchBar">
<color key="titleColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</state>
</button>
<searchBar contentMode="redraw" fixedFrame="YES" placeholder="Search" backgroundImage="blank" translatesAutoresizingMaskIntoConstraints="NO" id="dRT-vj-1ew">
<rect key="frame" x="8" y="15" width="326" height="44"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
@ -244,10 +249,12 @@
<viewLayoutGuide key="safeArea" id="Ut7-03-t0b"/>
</view>
<connections>
<outlet property="mapView" destination="Bex-Mk-DDO" id="2Ww-7N-bZc"/>
<outlet property="menuBtn" destination="m8G-AJ-moq" id="QXG-rH-6Lm"/>
<outlet property="menuView" destination="VZj-Lm-Ems" id="IBs-mV-GJW"/>
<outlet property="micBtn" destination="G7y-oE-2AD" id="W6R-nB-sax"/>
<outlet property="searchBar" destination="dRT-vj-1ew" id="nAa-5O-h0R"/>
<outlet property="searchBarBg" destination="3Xt-Rr-hs1" id="6bL-cK-ScI"/>
<outlet property="searchView" destination="bKl-Ku-mh1" id="d0n-Pl-NrK"/>
</connections>
</viewController>

View File

@ -7,16 +7,39 @@
//
import UIKit
import AVKit
import MapKit
import CoreLocation
import AVFoundation
class MainViewController: UIViewController {
class MainViewController: UIViewController, AVAudioPlayerDelegate {
let locationManager = CLLocationManager()
var currentCoordinate: CLLocationCoordinate2D!
var player = AVAudioPlayer()
var steps = [MKRoute.Step]()
var stepCounter = 0
var previousDistanceToWaypoint: Double = 1000
var distanceToNextCoord: Double = 1000
var startedNavigation: Bool = false
// OUTLETS--------------OUTLETS
@IBOutlet weak var menuView: UIView!
@IBOutlet weak var menuBtn: UIButton!
// ----------------------------
// ---------------------
@IBOutlet weak var searchBarBg: UIButton!
@IBOutlet weak var searchView: UIView!
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var micBtn: UIButton!
// ---------------------
@IBOutlet weak var mapView: MKMapView!
// OUTLETS--------------OUTLETS
// ACTIONS--------------ACTIONS
@ -30,10 +53,212 @@ class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.searchBarBg.layer.shadowColor = UIColor.black.cgColor
self.searchBarBg.layer.cornerRadius = 10
self.searchBarBg.layer.shadowOffset = CGSize(width: 5, height: 7)
self.searchBarBg.layer.shadowRadius = 10
self.searchBarBg.layer.shadowOpacity = 0.2
locationManager.delegate = (self as! CLLocationManagerDelegate)
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
} //END VIEW DID LOAD
func getDirections(to destination: MKMapItem) {
let sourcePlacemark = MKPlacemark(coordinate: currentCoordinate)
let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
let directionsRequest = MKDirections.Request()
directionsRequest.source = sourceMapItem
directionsRequest.destination = destination
directionsRequest.transportType = .walking
let directions = MKDirections(request: directionsRequest)
directions.calculate { (response, _) in
guard let response = response else { return }
guard let primaryRoute = response.routes.first else { return }
self.mapView.addOverlay(primaryRoute.polyline)
self.steps = primaryRoute.steps
for i in 0 ..< primaryRoute.steps.count {
let step = primaryRoute.steps[i]
print(step.instructions)
print(step.distance)
// -----------------------------Geofencing setup
let region = CLCircularRegion(center: step.polyline.coordinate,
radius: 15,
identifier: "\(i)")
// self.locationManager.startMonitoring(for: region)
// -----------------------------Geofencing setup
let circle = MKCircle(center: region.center, radius: region.radius)
self.mapView.addOverlay(circle)
}
self.startedNavigation = true
self.stepCounter += 1
}
}
} //END OF CLASS
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let currentLocation = locations.first else { return }
currentCoordinate = currentLocation.coordinate
mapView.userTrackingMode = .follow
if startedNavigation == true {
StepLabel.text = "On step: \(stepCounter)"
let nextStep = steps[stepCounter]
let nextCoord = CLLocation(latitude: nextStep.polyline.coordinate.latitude, longitude: nextStep.polyline.coordinate.longitude)
distanceToNextCoord = currentLocation.distance(from: nextCoord)
devonLbl.text = String(format: "Distance to next waypoint: %.2f meters", distanceToNextCoord)
a.text = String(format: "%.2f", previousDistanceToWaypoint)//"\(previousDistanceToWaypoint)"
b.text = String(format: "%.2f", distanceToNextCoord)//"\(distanceToNextCoord)"
if distanceToNextCoord < previousDistanceToWaypoint {
previousDistanceToWaypoint = currentLocation.distance(from: nextCoord)
}
if distanceToNextCoord > previousDistanceToWaypoint + 10 {
devonLbl.text = String(format: "WRONG WAY (%.2f > %.2f)", distanceToNextCoord, previousDistanceToWaypoint+10)
previousDistanceToWaypoint = distanceToNextCoord - 5
playTurnArd()
}
if distanceToNextCoord < 15 {
stepCounter += 1
if stepCounter < steps.count {
// let currentStep = steps[stepCounter]
let message = "\(steps[stepCounter-1].instructions)"
directionsLabel.text = message
let maneuverCommand = String(message.prefix(10))
switch (maneuverCommand) {
case "Turn right":
playRight()
break;
case "Bear right":
playRight()
break;
case "Turn left ":
playLeft()
break;
case "Bear left":
playLeft()
break;
case "The destin":
playDone()
default:
playErr()
currentStepLbl.text = "ERR: \(message)"
break;
}
// let speechUtterance = AVSpeechUtterance(string: message)
// speechSynthesizer.speak(speechUtterance)
previousDistanceToWaypoint = 1000
} else {
let message = "Arrived at destination"
directionsLabel.text = message
// let speechUtterance = AVSpeechUtterance(string: message)
// speechSynthesizer.speak(speechUtterance)
stepCounter = 0
previousDistanceToWaypoint = 1000
cancelView.isHidden = true
searchBar.isHidden = false
// locationManager.monitoredRegions.forEach({ self.locationManager.stopMonitoring(for: $0) })
}
}
}
}
func playRight(){
// currentStepLbl.text = "COM: RIGHT"
let path = Bundle.main.path(forResource: "xright", ofType : "mp3")!
let url = URL(fileURLWithPath : path)
do {
player = try AVAudioPlayer(contentsOf: url)
player.delegate = self
player.play()
} catch {}
}
func playLeft(){
// currentStepLbl.text = "COM: LEFT"
let path = Bundle.main.path(forResource: "xleft", ofType : "mp3")!
let url = URL(fileURLWithPath : path)
do {
player = try AVAudioPlayer(contentsOf: url)
player.delegate = self
player.play()
} catch {}
}
func playErr(){
// currentStepLbl.text = "COM: ERROR!!!"
return
}
func playTurnArd(){
// currentStepLbl.text = "COM: UTURN"
let path = Bundle.main.path(forResource: "xboth", ofType : "mp3")!
let url = URL(fileURLWithPath : path)
do {
player = try AVAudioPlayer(contentsOf: url)
player.delegate = self
player.play()
} catch {}
}
func playDone(){
// currentStepLbl.text = "COM: DONE"
}
}
extension ViewController: UISearchBarDelegate {
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.endEditing(true) //HIDES LE KEYBOARD
let localSearchRequest = MKLocalSearch.Request()
localSearchRequest.naturalLanguageQuery = searchBar.text
let region = MKCoordinateRegion(center: currentCoordinate, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
localSearchRequest.region = region
let localSearch = MKLocalSearch(request: localSearchRequest)
localSearch.start { (response, _) in
guard let response = response else { return }
guard let firstMapItem = response.mapItems.first else { return }
self.getDirections(to: firstMapItem)
}
}
}
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKPolyline {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = .blue
renderer.alpha = 0.7
renderer.lineWidth = 10
return renderer
}
if overlay is MKCircle {
let renderer = MKCircleRenderer(overlay: overlay)
renderer.strokeColor = .red
renderer.fillColor = .red
renderer.alpha = 0.4
return renderer
}
return MKOverlayRenderer()
}
}