yufan
yufan

Reputation: 23

swift mapkit current location

I am using MapKit to get the current location but it keeps getting none location

import UIKit
import MapKit

class NewLocationController: UIViewController,CLLocationManagerDelegate {

    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var descriptionTextField: UITextField!
    @IBOutlet weak var latitudeTextField: UITextField!
    @IBOutlet weak var longtitudeTextField: UITextField!
    var delegate: newLocationDelegate?
    var locationManager: CLLocationManager = CLLocationManager()    
    var currentLocation: CLLocationCoordinate2D?

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.distanceFilter = 10
        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
        // Do any additional setup after loading the view.
    }

    func locationManager (_ manager: CLLocationManager, didUpdateLocations locations:[CLLocation]){
        let loc: CLLocation = locations.last!
        currentLocation = loc.coordinate
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func addNewLocation(title: String, description: String, lat: Double, long: Double){
        let location : FencedAnnotation = FencedAnnotation(newTitle: title, newSubtitle: description, lat: lat, long: long)
        delegate!.didSaveLocation(location)
        self.navigationController?.popViewController(animated: true)
    }

    @IBAction func saveNewAnimal(_ sender: Any) {
        addNewLocation(title: nameTextField.text!, description: descriptionTextField.text!, lat:Double(latitudeTextField.text!)!, long:Double(longtitudeTextField.text!)!)
    }

    @IBAction func saveCurrentLocation(_ sender: Any) {
        self.latitudeTextField.text = "\(currentLocation!.latitude)"
        self.longtitudeTextField.text = "\(currentLocation!.longitude)"
    }

Upvotes: 1

Views: 121

Answers (1)

Daniel Larsson
Daniel Larsson

Reputation: 527

Try to import CoreLocation, call the mLocation in viewDidLoad. LocationManager updates your location after the mLocation function is done.

if distance method updates right now every 100 meters.

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    let myLocation = CLLocation()
    let locationManager = CLLocationManager()

    @IBOutlet weak var latitudeTextField: UITextField!
    @IBOutlet weak var longtitudeTextField: UITextField!

    override func viewDidLoad(){
        super.viewDidLoad()
        mLocation()
    }

    @IBAction func saveCurrentLocation(){
        self.latitudeTextField.text = "\\(myLocation.coordinate.latitude)"
        self.longtitudeTextField.text = "\(myLocation.coordinate.longitude)"

    }

    func mLocation(){
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()

        if CLLocationManager.locationServicesEnabled(){
            locationManager.startUpdatingLocation()
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations:[CLLocation]){
        let newLocation = locations[0]
        let distance = myLocation.distance(from: newLocation)
        if distance > 100 {
            myLocation = newLocation
        }
    }
}

Upvotes: 2

Related Questions