Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // FourthViewController.swift
- // dzbanylab3
- //
- // Created by ZUI on 22.05.2018.
- // Copyright © 2018 SCLab. All rights reserved.
- //
- import UIKit
- import CoreLocation
- class FourthViewController: UIViewController,CLLocationManagerDelegate {
- var location: CLLocation?
- let locationManager = CLLocationManager()
- let geocoder = CLGeocoder()
- var placemark: CLPlacemark?
- var performingReverseGeocoding = false;
- var lastGeocodingError: Error?
- @IBOutlet weak var longitudeLabel: UILabel!
- @IBOutlet weak var latitudeLabel: UILabel!
- @IBOutlet weak var Label: UILabel!
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- func locationName(from placemark: CLPlacemark) -> String { return
- "\(placemark.subThoroughfare!) \(placemark.thoroughfare!)\n" +
- "\(placemark.locality!) \(placemark.administrativeArea!) " +
- "\(placemark.postalCode!)"
- }
- func locationManager(_ manager: CLLocationManager,
- didFailWithError error: Error) { print("didFailWithError \(error)")
- }
- func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
- let newLocation = locations.last!
- print("didUpdateLocations \(newLocation)")
- location = newLocation
- if let location = location{
- latitudeLabel.text = "Szerokosc: " + String(format: "%.8f", location.coordinate.latitude)
- longitudeLabel.text = "Dlugosc: " + String(format: "%.8f", location.coordinate.longitude) }
- // Reverse Geocoding
- if !performingReverseGeocoding {
- print(" Wykonuję geokodowanie ")
- performingReverseGeocoding = true
- geocoder.reverseGeocodeLocation(location!, completionHandler: {
- placemarks, error in print("*** Znalazłem miejsce: \(placemarks), error: \(error)"); if error == nil, let p = placemarks, !p.isEmpty {
- self.placemark = p.last!
- self.Label.text = self.locationName(from: self.placemark!)
- self.performingReverseGeocoding = false
- } else { self.placemark = nil }
- })
- }
- }
- @IBAction func getLocation()
- {
- let authStatus = CLLocationManager.authorizationStatus(); if authStatus == .notDetermined {
- locationManager.requestWhenInUseAuthorization()
- return }
- locationManager.delegate = self
- locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
- locationManager.startUpdatingLocation()
- }
- //dodaj w domu obsluge klawisza
- /*
- // MARK: - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- // Get the new view controller using segue.destinationViewController.
- // Pass the selected object to the new view controller.
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement