SWIFT - LocationManager looping through multiple times?

后端 未结 2 1106
旧时难觅i
旧时难觅i 2020-12-21 13:33

I have a locationManager function to grab the users current location and posting the name of the city and state. I have a print statement so I can check in my console if eve

2条回答
  •  时光说笑
    2020-12-21 13:52

    I had the same problem and Rob's answer didn't do it for me.

    When the location service first starts, the location is updated multiple times regardless of the distanceFilter.

    You might still want the location to be updated and you don't want to lose the location accuracy(which is the whole point of updating location multiple times on start-up), so calling stopUpdatingLocation(or using a local variable) after the first geolocating call isn't the way to go either.

    The most intuitive way is to wrap your geocode call in an @objc function and call the the function with a delay:

    NSObject.cancelPreviousPerformRequests(withTarget: self)
    perform(#selector(myGeocodeFunction(_:)), with: location, afterDelay: 0.5)
    

提交回复
热议问题