我有以下SWIFT 3代码,可以使用MAPKIT和CORELOCATE找到当前位置,但它不起作用.我做错了什么



以下是我的Swift 3项目中的ViewController.swift文件的代码片段。我应该改变什么才能使它起作用,或者您可以给任何指示?您知道为什么它不起作用吗?相反,在完成当前位置的通知之后,去一些静脉位置而不是我的位置?

import Cocoa
import MapKit
import CoreLocation
class ViewController: NSViewController, CLLocationManagerDelegate , MKMapViewDelegate{
    @IBOutlet weak var STAVO_map: MKMapView!
    var manager: CLLocationManager!
    var currentLocation : CLLocation!
    override func viewDidLoad() {
        super.viewDidLoad()
        manager = CLLocationManager()
        STAVO_map.delegate = self
        STAVO_map.showsUserLocation = true
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.delegate = self
        if CLLocationManager.locationServicesEnabled() {
            manager.startUpdatingLocation()
        }
        DispatchQueue.main.async {
            self.manager.startUpdatingLocation()
        }

    }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location = locations.last as! CLLocation
        self.manager.stopUpdatingLocation()
        let region = MKCoordinateRegionMakeWithDistance(location.coordinate, 500, 500)
        self.STAVO_map.setRegion(region, animated: true)
    }
    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }

}

一些事情,首先我会将didupdatelecations更改为

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations.last as! CLLocation
    manager.stopUpdatingLocation()
    let region = MKCoordinateRegionMakeWithDistance(location.coordinate, 1000, 1000)

    self.STAVO_map.setRegion(region, animated: true)
}

在ViewDidload中……启动updatingLocation是一个异步函数,因此您始终将设置为单位,直到调用DIDUPDATELACATIONS。我将删除您设置NoLocation,ViewRecion和SetRecion的3行。您不需要两次致电startupdatinglocation,以便您删除第二个呼叫(此外,默认情况下在主队列中运行的ViewDidload,因此无需派遣派遣(

相关内容

最新更新