Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /
- // ViewController.swift
- // SCD
- //
- // Created by SabauCristina on 05/01/2018.
- // Copyright © 2018 SabauCristina. All rights reserved.
- //
- import UIKit
- import CoreLocation
- import Alamofire
- let baseUrl = "http://localhost:3000/api/positions"
- class ViewController: UIViewController {
- @IBOutlet weak var locationLabel: UILabel!
- @IBOutlet weak var sendButton: UIButton!
- var location: [String: Double] = [:]
- let locationManager = CLLocationManager()
- override func viewDidLoad() {
- super.viewDidLoad()
- locationManager.requestWhenInUseAuthorization()
- if CLLocationManager.locationServicesEnabled() {
- locationManager.delegate = self
- locationManager.desiredAccuracy = kCLLocationAccuracyBest
- locationManager.startUpdatingLocation()
- let _ = Timer.scheduledTimer(timeInterval: 20, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)
- }
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- }
- @IBAction func didTapLocation(_ sender: Any) {
- update()
- }
- @objc func update() {
- let headers: HTTPHeaders = [
- "Content-Type": "application/json",
- "Authorization":"Token token=tHJb1ZTiFTYDLZjUtoRLnjmu, email=cristina@yahoo.com"
- ]
- Alamofire.request(baseUrl, method: .post, parameters: ["latitude": location["latitude"] ?? 0, "longitude": location["longitude"] ?? 0 , "user_id": 1],encoding: JSONEncoding.default, headers: headers).responseJSON {
- response in
- switch response.result {
- case .success:
- debugPrint(response)
- break
- case .failure(let error):
- debugPrint(error)
- }
- }
- }
- }
- extension ViewController: CLLocationManagerDelegate {
- func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
- guard let loc = manager.location else {
- return
- }
- let locValue = loc.coordinate
- location["latitude"] = locValue.latitude
- location["longitude"] = locValue.longitude
- locationLabel.text = "lat = \(locValue.latitude) long = \(locValue.longitude)"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement