我制作了一个应用程序,用于显示用户位置的坐标。它与Wifi连接工作正常但在没有互联网连接的情况下等待2-3分钟后无法显示任何内容。我很好奇,Compass,iphone的内置应用程序可以在校准后的几秒钟内显示坐标。
我的以下代码是用swift编写的,它根据GPS的坐标查找地址。我很困惑为什么这段代码在没有互联网连接的情况下无法显示纬度和经度。
非常感谢任何帮助。谢谢!
import UIKit
import CoreLocation
class ViewController2: UIViewController, CLLocationManagerDelegate
{
@IBOutlet var locality: UILabel?
@IBOutlet var postalCode: UILabel?
@IBOutlet var AdministrativeArea: UILabel?
@IBOutlet var country: UILabel?
@IBOutlet var latitude: UILabel?
@IBOutlet var longitude: UILabel?
var currentLocation = CLLocation()
let locationManager = CLLocationManager()
override func viewDidLoad()
{
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
currentLocation = locationManager.location
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
{
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error)->Void in
if (error != nil)
{
println("Error: " + error.localizedDescription)
return
}
if placemarks.count > 0
{
let pm = placemarks[0] as! CLPlacemark
self.displayLocationInfo(pm)
}
else
{
println("Error with the data.")
}
})
}
func displayLocationInfo(placemark: CLPlacemark)
{
/
locality?.text = "\(placemark.locality)"
postalCode?.text = "\(placemark.postalCode)"
AdministrativeArea?.text = "\(placemark.administrativeArea)"
country?.text = "\(placemark.country)"
latitude?.text = "\(currentLocation.coordinate.latitude)"
longitude?.text = "\(currentLocation.coordinate.longitude)"
}