简体   繁体   中英

Can't get user's current location

I have a simple goal of my app that is get the current coordinate, and use them to add an annotation on the mapview.

I have been tried lots of solution from google results, but its still not working....

The debug area never shows "locationManager did UpdateLocation", the message what I print in function....

It's seems like the app never run "did UpdateLocation" function, even startUpdatingLocation() has been called?


Add location privacy string in info.plist : Done.

Turn on the GPS on my Mac Pro : Done.

Xcode version : 10.1

MacOS : 10.13.6 (High Sierra)


let cloaction = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    MapView.delegate = self
    MapView.showsScale = true
    MapView.showsPointsOfInterest = true
    MapView.showsUserLocation = true

    cloaction.requestAlwaysAuthorization()
    cloaction.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        print("IN")

        cloaction.delegate = self
        cloaction.desiredAccuracy = kCLLocationAccuracyBest
        cloaction.startUpdatingLocation()
    }        
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    print("locationManager did UpdateLocation")

    let location = CLLocationCoordinate2D(latitude: (locations.first?.coordinate.latitude)!, longitude: (locations.first?.coordinate.longitude)!)

    currentLat = (locations.first?.coordinate.latitude)!
    currentLon = (locations.first?.coordinate.longitude)!

    let span = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)

    MapView.setRegion(MKCoordinateRegion(center: CLLocationCoordinate2DMake(currentLat,currentLon), span: span), animated: true)
    MapView.showsUserLocation = true

    print(locations.first?.coordinate.latitude)
    print(locations.first?.coordinate.longitude)

}

Have you import CoreLocation ?

Start with making a variable let myLocation = CLLocation()

Instead of have so much in viewDidLoad you can make a function and call the mLocation in viewDidLoad instead :

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

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

And thats all you need for the clocation

LocationManager could also be updated

 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations:[CLLocation]){
        let newLocation = locations[0]

        print("\(myLocation.coordinate.latitude)")
        print("\(myLocation.coordinate.longitude)")
    }

Actually the reason is very simple: You call the didUpdateLocation mehthod wich is only called when you change your location. Your Mac is on certain place and dont move so thats why it is not working.

Yes! finally... thank your answering, it's really need to run on device, thanks Kosuke Ogawa's suggestion, and every one's guide, I am a new to learn swift, and first time ask question here, it's fun, thank you every one. (But I don't know how to accept a answer if the answer is a comment? Someone teach me how do that?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM