3

Using the following code, my currentLat and currentLong are not updating beyond the first location. locations never has more than 1 item, and it's always exactly the same.

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let loc: CLLocation = locations[locations.count - 1]
    currentLat = loc.coordinate.latitude
    currentLong = loc.coordinate.longitude
}

All the searching I've been doing has only shown how to get it to work if it doesn't work at all, but this DOES work, but only once.

The way I expect this to work is to continuously update the GPS coordinates and fire this delegate off whenever the location is updated. Does the location keep updating the entire time? I thought it would due to the terminology of "startUpdatingLocation()" and there being a "stopUpdatingLocation()." Am I trying to use this incorrectly?

Like I said, it works for the first time, so it's loaded and started just fine, the permissions are allowed, the info.plist has the necessary entries.

sdouble
  • 1,055
  • 3
  • 11
  • 28
  • Are you calling the delegate correctly in your **viewDidLoad**? – Brian Nezhad Oct 30 '15 at 02:45
  • Yes, or it wouldn't be working the first time. That's also where I have the startUpdatingLocation() – sdouble Oct 30 '15 at 02:48
  • try to stop the update after first update `stopUpdatingLocation()` put it before `let loc: CLLocation` and then call the update location again. – Brian Nezhad Oct 30 '15 at 02:50
  • Please have a look into given solution: [Update user location objective-c and Swift](http://stackoverflow.com/a/32044306/3024579) – Alok Jun 27 '16 at 06:23

1 Answers1

4

When you want to update your location, you should stop existing update once it got new location. You can use stopUpdatingLocation() in your didUpdateLocations: method to do so.

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

    // This should match your CLLocationManager()
    locManager.stopUpdatingLocation()

    let loc: CLLocation = locations[locations.count - 1]
    currentLat = loc.coordinate.latitude
    currentLong = loc.coordinate.longitude
}

Then call your locManager.startUpdatingLocation() when you need new location.

Brian Nezhad
  • 6,148
  • 9
  • 44
  • 69
  • 1
    I see what you're saying. So startUpdatingLocation() is actually only a single time use, and won't keep going in the background. From what I read, I assumed it was going to keep updating the location and every time it did, it would execute this delegate. This would explain why I have no problem getting the first location, but never again. I only call startupdatingLocation() in viewDidLoad(). – sdouble Oct 30 '15 at 03:10
  • 1
    Exactly, It makes sense if you think about it, once you got the location, you haven't stopped it so basically is same as first update. I hope I get Green Check on this Answer, Cheers Mate :) – Brian Nezhad Oct 30 '15 at 03:11
  • What? What is that? @LeoDabus Kill it now. – Brian Nezhad Oct 30 '15 at 03:23
  • 1
    Most excellent. This solves my problem. All of the examples I ran across were only getting coordinates once, so the example wasn't clear enough for what I was trying to do. It makes sense to me now, thanks Farhad. – sdouble Oct 30 '15 at 03:30