我有一个locationManager函数来获取用户当前位置并发布城市和州的名称.我有一个打印声明,所以我可以在我的控制台检查一切是否正常工作......就是这样.但是,它打印城市位置3次.这实际上在我的实际应用程序中引起了一个问题,但这超出了这个问题的范围.
我的功能如下:
var usersLocation: String!
var locationManager: CLLocationManager!
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation: CLLocation = locations[0]
CLGeocoder().reverseGeocodeLocation(userLocation) { (placemarks, error) -> Void in
if error != nil {
print(error)
} else {
let p = placemarks?.first // ".first" returns the first element in the collection, or nil if its empty
// this code above will equal the first element in the placemarks array
let city = p?.locality != nil ? p?.locality : ""
let state = …Run Code Online (Sandbox Code Playgroud)