Advertisement
RybaSG

FourthViewController

Jun 2nd, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.28 KB | None | 0 0
  1. //
  2. //  FourthViewController.swift
  3. //  dzbanylab3
  4. //
  5. //  Created by ZUI on 22.05.2018.
  6. //  Copyright © 2018 SCLab. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import CoreLocation
  11.  
  12. class FourthViewController: UIViewController,CLLocationManagerDelegate {
  13.    
  14.     var location: CLLocation?
  15.     let locationManager = CLLocationManager()
  16.    
  17.     let geocoder = CLGeocoder()
  18.     var placemark: CLPlacemark?
  19.     var performingReverseGeocoding = false;
  20.     var lastGeocodingError: Error?
  21.    
  22.     @IBOutlet weak var longitudeLabel: UILabel!
  23.     @IBOutlet weak var latitudeLabel: UILabel!
  24.     @IBOutlet weak var Label: UILabel!
  25.    
  26.     override func viewDidLoad() {
  27.         super.viewDidLoad()
  28.  
  29.         // Do any additional setup after loading the view.
  30.     }
  31.  
  32.     override func didReceiveMemoryWarning() {
  33.         super.didReceiveMemoryWarning()
  34.         // Dispose of any resources that can be recreated.
  35.     }
  36.    
  37.     func locationName(from placemark: CLPlacemark) -> String { return
  38.         "\(placemark.subThoroughfare!) \(placemark.thoroughfare!)\n" +
  39.             "\(placemark.locality!) \(placemark.administrativeArea!) " +
  40.         "\(placemark.postalCode!)"
  41.     }
  42.     func locationManager(_ manager: CLLocationManager,
  43.                          didFailWithError error: Error) { print("didFailWithError \(error)")
  44.     }
  45.     func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  46.         let newLocation = locations.last!
  47.         print("didUpdateLocations \(newLocation)")
  48.         location = newLocation
  49.         if let location = location{
  50.             latitudeLabel.text = "Szerokosc: " + String(format: "%.8f", location.coordinate.latitude)
  51.             longitudeLabel.text = "Dlugosc: " + String(format: "%.8f", location.coordinate.longitude) }
  52.        
  53.         // Reverse Geocoding
  54.         if !performingReverseGeocoding {
  55.             print(" Wykonuję geokodowanie ")
  56.                 performingReverseGeocoding = true
  57.             geocoder.reverseGeocodeLocation(location!, completionHandler: {
  58.                 placemarks, error in print("*** Znalazłem miejsce: \(placemarks), error: \(error)"); if error == nil, let p = placemarks, !p.isEmpty {
  59.                 self.placemark = p.last!
  60.                 self.Label.text = self.locationName(from: self.placemark!)
  61.                 self.performingReverseGeocoding = false
  62.                 } else { self.placemark = nil }
  63.                 })
  64.         }
  65.        
  66.     }
  67.    
  68.    
  69.     @IBAction func getLocation()
  70.     {
  71.         let authStatus = CLLocationManager.authorizationStatus(); if authStatus == .notDetermined {
  72.             locationManager.requestWhenInUseAuthorization()
  73.             return }
  74.         locationManager.delegate = self
  75.         locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
  76.         locationManager.startUpdatingLocation()
  77.     }
  78.    
  79.  
  80.         //dodaj w domu obsluge klawisza
  81.     /*
  82.     // MARK: - Navigation
  83.  
  84.     // In a storyboard-based application, you will often want to do a little preparation before navigation
  85.     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  86.         // Get the new view controller using segue.destinationViewController.
  87.         // Pass the selected object to the new view controller.
  88.     }
  89.     */
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement