swift mapkit текущее местоположение - PullRequest
0 голосов
/ 25 августа 2018

Я использую MapKit, чтобы получить текущее местоположение, но оно не получает никакого местоположения

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)"
    }

1 Ответ

0 голосов
/ 19 октября 2018

Попробуйте import CoreLocation, наберите mLocation в viewDidLoad. LocationManager обновляет ваше местоположение после выполнения функции mLocation.

if distance метод обновляется прямо сейчас каждые 100 метров.

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
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...