Advertisement
Guest User

Untitled

a guest
Jan 14th, 2018
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.41 KB | None | 0 0
  1. /
  2. //  ViewController.swift
  3. //  SCD
  4. //
  5. //  Created by SabauCristina on 05/01/2018.
  6. //  Copyright © 2018 SabauCristina. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import CoreLocation
  11. import Alamofire
  12.  
  13. let baseUrl = "http://localhost:3000/api/positions"
  14.  
  15. class ViewController: UIViewController {
  16.    
  17.     @IBOutlet weak var locationLabel: UILabel!
  18.     @IBOutlet weak var sendButton: UIButton!
  19.    
  20.     var location: [String: Double] = [:]
  21.    
  22.     let locationManager = CLLocationManager()
  23.    
  24.     override func viewDidLoad() {
  25.         super.viewDidLoad()
  26.        
  27.         locationManager.requestWhenInUseAuthorization()
  28.        
  29.         if CLLocationManager.locationServicesEnabled() {
  30.             locationManager.delegate = self
  31.             locationManager.desiredAccuracy = kCLLocationAccuracyBest
  32.             locationManager.startUpdatingLocation()
  33.            
  34.             let _ = Timer.scheduledTimer(timeInterval: 20, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)
  35.            
  36.         }
  37.        
  38.     }
  39.  
  40.     override func didReceiveMemoryWarning() {
  41.         super.didReceiveMemoryWarning()
  42.     }
  43.    
  44.     @IBAction func didTapLocation(_ sender: Any) {
  45.         update()
  46.     }
  47.    
  48.     @objc func update() {
  49.         let headers: HTTPHeaders = [
  50.             "Content-Type": "application/json",
  51.             "Authorization":"Token token=tHJb1ZTiFTYDLZjUtoRLnjmu, email=cristina@yahoo.com"
  52.         ]
  53.         Alamofire.request(baseUrl, method: .post, parameters: ["latitude": location["latitude"] ?? 0, "longitude": location["longitude"] ?? 0 , "user_id": 1],encoding: JSONEncoding.default, headers: headers).responseJSON {
  54.             response in
  55.             switch response.result {
  56.             case .success:
  57.                 debugPrint(response)
  58.                 break
  59.             case .failure(let error):
  60.                
  61.                 debugPrint(error)
  62.             }
  63.         }
  64.     }
  65. }
  66.  
  67. extension ViewController: CLLocationManagerDelegate {
  68.    
  69.     func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  70.         guard let loc = manager.location else {
  71.             return
  72.         }
  73.         let locValue = loc.coordinate
  74.         location["latitude"] = locValue.latitude
  75.         location["longitude"] = locValue.longitude
  76.         locationLabel.text = "lat = \(locValue.latitude) long = \(locValue.longitude)"
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement