Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.87 KB | None | 0 0
  1. //
  2. //  ViewController.swift
  3. //  LocationTest
  4. //
  5. //  Created by Oscar Bjurelid on 2018-11-12.
  6. //  Copyright © 2018 Oscar Bjurelid. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import CoreLocation
  11.  
  12.  
  13. class ViewController: UIViewController, CLLocationManagerDelegate {
  14.    
  15.     var locationManager: CLLocationManager!
  16.    
  17.  
  18.    
  19.    
  20.     @IBOutlet weak var Location: UILabel!
  21.     @IBOutlet weak var Coords: UILabel!
  22.    
  23.     override func viewDidLoad() {
  24.         super.viewDidLoad()
  25.        
  26.         initLocationManager()
  27.        
  28.        
  29.         var myLocation = CLLocation()
  30.        
  31.         print(locationManager.location?.coordinate)
  32.        
  33.         print(myLocation.altitude)
  34.        
  35.         var myLon = locationManager.location?.coordinate
  36.         var myLat = locationManager.location?.coordinate.latitude
  37.        
  38.         Location.text = locationDict
  39.        
  40.        
  41.         }
  42.    
  43.     func initLocationManager() {
  44.         locationManager = CLLocationManager()
  45.         locationManager.delegate = self
  46.         locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
  47.         locationManager.distanceFilter = 1000
  48.         locationManager.requestWhenInUseAuthorization()
  49.         locationManager.startUpdatingLocation()
  50.    
  51.        
  52.     }
  53.    
  54.     func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  55.        
  56.         self.locationManager.stopUpdatingLocation()
  57.         var locationArray = locations as NSArray
  58.         var locationObj = locationArray.lastObject as! CLLocation
  59.         var coord = locationObj.coordinate
  60.        
  61.         let latitude : NSNumber = NSNumber(value: coord.latitude)
  62.         let longitude : NSNumber = NSNumber(value: coord.longitude)
  63.        
  64.         let locationDict = ["latitude": latitude, "longitude": longitude]
  65.        
  66.    
  67.    
  68.    
  69.    
  70.    
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement