在尝试获取位置时,它只是我还是iOS模拟器很慢?有时需要多次调用才能获取信息。
例如,我将位置设置为纽约,更新我的标签以显示“纽约,纽约”,将位置更改为香港,并尝试再次更新我的标签,但需要2到5个方法调用在标签变为香港之前。
我的位置代码效率低下还是这些请求的iOS模拟器错误?
@IBAction func findMyLocation(sender: AnyObject) {
// set location manager, set accuracy and start updating
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
CLGeocoder().reverseGeocodeLocation(manager.location) {
(let placemarks, let error) in
if error != nil {
println("Reverse geocoder failed with error" + error.localizedDescription)
return
}
if placemarks.count > 0 && placemarks != nil {
let placemark = placemarks[0] as! CLPlacemark
let locationsArray = locations as NSArray
self.displayLocationInfo(placemark, locations: locationsArray)
}
}
}
func displayLocationInfo(placemark: CLPlacemark, locations: NSArray) {
// stop updating to save power
locationManager.stopUpdatingLocation()
cityLabel?.text = placemark.locality
zipcodeLabel?.text = placemark.postalCode
stateLabel?.text = placemark.administrativeArea
countryLabel?.text = placemark.country
if let locationObj = locations.lastObject as? CLLocation {
let coordinates = locationObj.coordinate
coordinatesLabel?.text = "\(coordinates.latitude),\(coordinates.longitude)"
}
}
如果有更好的方法来编写我的代码,请发布!我是iOS开发的新手!