Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MapViewController: UIViewController {
- @IBOutlet weak var mapView: MKMapView!
- var route: MKRoute!
- var latestLocation: CLLocation?
- let locationDelegate = LocationDelegate()
- let destination: CLLocation = CLLocation(latitude: 40.851652, longitude: 14.268238)//40.851652, 14.268238
- var isCalculateButtonTapped: Bool = false
- var stepMaxDistance: Double = 100000
- @IBOutlet weak var stattBuonLabel: UILabel!
- // @IBOutlet weak var embViewCompass: UIView!
- // @IBOutlet weak var imageViewCompass: UIImageView!
- // @IBOutlet weak var txtDistance: UILabel!
- // @IBOutlet weak var btnCalculate: UIButton!
- // @IBOutlet weak var txtWalkingDistance: UILabel!
- @IBOutlet weak var btnCompass: UIButton!
- var yourLocationBearing: CGFloat { return latestLocation?.bearingToLocationRadian(self.yourLocation) ?? 0 }
- var yourLocation: CLLocation {
- get { return UserDefaults.standard.currentLocation }
- set { UserDefaults.standard.currentLocation = newValue }
- }
- let locationManager: CLLocationManager = {
- $0.requestWhenInUseAuthorization()
- $0.desiredAccuracy = kCLLocationAccuracyBestForNavigation
- $0.startUpdatingLocation()
- $0.startUpdatingHeading()
- return $0
- }(CLLocationManager())
- private func orientationAdjustment() -> CGFloat {
- let isFaceDown: Bool = {
- switch UIDevice.current.orientation {
- case .faceDown: return true
- default: return false
- }
- }()
- let adjAngle: CGFloat = {
- switch UIApplication.shared.statusBarOrientation {
- case .landscapeLeft: return 90
- case .landscapeRight: return -90
- case .portrait, .unknown: return 0
- case .portraitUpsideDown: return isFaceDown ? 180 : -180
- }
- }()
- return adjAngle
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- mapView.delegate = self
- // Do any additional setup after loading the view, typically from a nib.
- mapView.showsUserLocation = true
- mapView.userTrackingMode = .followWithHeading
- // self.MapViewController.childContaining(CompassViewController)
- // print(self.children.count)
- locationManager.delegate = locationDelegate
- locationDelegate.locationCallback = { location in
- self.latestLocation = location
- // vedere se questo codice centra la posizione
- // let region = MKCoordinateRegion(center:
- // CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude),
- // span: MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002))
- // self.mapView.setRegion(region, animated: false) // con animated true, centra la posizione corrente
- // calcola la distanza aerea tra due coordinate
- let distance = self.destination.distance(from: location)
- // self.txtDistance.text = "\(distance.magnitude)"//distance.description
- if self.isCalculateButtonTapped {
- let stepNumber = self.route.steps.count - 1
- var nearestStep = 0
- for stepIndex in nearestStep...stepNumber {
- if (self.route.steps[stepIndex].distance < self.route.steps[nearestStep].distance) {
- nearestStep = stepIndex
- }
- }
- self.stattBuonLabel.text = "step \(nearestStep) dista: \(self.route.steps[nearestStep].distance)"
- }
- }
- locationDelegate.headingCallback = { newHeading in
- func computeNewAngle(with newAngle: CGFloat) -> CGFloat {
- let heading: CGFloat = {
- let originalHeading = self.yourLocationBearing - newAngle.degreesToRadians
- switch UIDevice.current.orientation {
- case .faceDown: return -originalHeading
- default: return originalHeading
- }
- }()
- return CGFloat(self.orientationAdjustment().degreesToRadians + heading)
- }
- UIView.animate(withDuration: 0.5) {
- let angle = computeNewAngle(with: CGFloat(newHeading))
- // self.imageViewCompass.transform = CGAffineTransform(rotationAngle: angle)
- }
- }
- }
- @IBAction func onClickCompass(_ sender: Any) {
- let popOverVC = UIStoryboard(name: "MapLayers", bundle: nil).instantiateViewController(withIdentifier: "sbSettings") as! SettingsViewController
- self.addChild(popOverVC)
- popOverVC.view.frame = self.view.frame
- self.view.addSubview(popOverVC.view)
- popOverVC.didMove(toParent: self)
- }
- // funzione che si occupa di ruotare l'immagine in ase alla localizzazione
- // func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
- // UIView.animate(withDuration: 0.5) {
- //
- // let angle = newHeading.trueHeading.degreesToRadians // convert from degrees to radians
- //
- // func computeNewAngle(with newAngle: CGFloat) -> CGFloat {
- // let heading: CGFloat = {
- // let originalHeading = self.yourLocationBearing - newAngle.degreesToRadians
- // switch UIDevice.current.orientation {
- // case .faceDown: return -originalHeading
- // default: return originalHeading
- // }
- // }()
- //
- // return CGFloat(self.orientationAdjustment().degreesToRadians + heading)
- // }
- //
- // UIView.animate(withDuration: 0.5) {
- // let angle = computeNewAngle(with: CGFloat(newHeading))
- // self.imageView.transform = CGAffineTransform(rotationAngle: angle)
- // }
- //
- // }
- // }
- // da testare
- func getDistanceToDestination(srcMapItem srcmapItem: MKMapItem, destMapItem destmapItem: MKMapItem) {
- // self.embViewCompass
- let request = MKDirections.Request() //create a direction request object
- request.source = srcmapItem //this is the source location mapItem object
- request.destination = destmapItem //this is the destination location mapItem object
- request.transportType = MKDirectionsTransportType.walking //define the transportation method
- let directions = MKDirections(request: request) //request directions
- directions.calculate { (response, error) in
- guard let response = response else {
- print(error.debugDescription)
- return
- }
- self.route = response.routes[0] //get the routes, could be multiple routes in the routes[] array but usually [0] is the best route
- // self.txtWalkingDistance.text = self.route.distance.description
- self.mapView.addOverlay((self.route.polyline), level: MKOverlayLevel.aboveRoads)
- let rect = self.route.polyline.boundingMapRect
- self.mapView.setRegion(MKCoordinateRegion(rect), animated: true)
- }
- }
- func calculateDistance(){
- guard let userLocation = latestLocation else { return }
- let myCoordinates = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude ?? 40.833399, longitude: userLocation.coordinate.longitude ?? 14.201540)
- let myPlacemark = MKPlacemark(coordinate: myCoordinates)
- let myMapItem = MKMapItem(placemark: myPlacemark)
- let myCoordinates2 = CLLocationCoordinate2D(latitude: destination.coordinate.latitude, longitude: destination.coordinate.longitude)
- let myPlacemark2 = MKPlacemark(coordinate: myCoordinates2)
- let myMapItem2 = MKMapItem(placemark: myPlacemark2)
- yourLocation = destination
- self.getDistanceToDestination(srcMapItem: myMapItem, destMapItem: myMapItem2)
- }
- @IBAction func onClickCalculate(_ sender: Any) {
- isCalculateButtonTapped = true
- calculateDistance()
- // casa 40.833399, 14.201540
- }
- }
- extension MapViewController: MapViewControllerDelegate, MKMapViewDelegate {
- func update(location: CLLocation) {
- yourLocation = location
- }
- func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
- let renderer = MKPolylineRenderer(overlay: overlay)
- renderer.strokeColor = .blue
- renderer.lineWidth = 10.0
- // let locationMapPoint = MKMapPoint((latestLocation?.coordinate)!)
- // let locationCGPoint = renderer.point(for: locationMapPoint)
- return renderer
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement