Hi,
I have made an app that will display the co-ordinates of the user location. It works fine with Wifi connectivity but fails to display anything despite waiting for 2-3 minutes, when there isn't internet connection and even when clear of obstructions. I was curious how Compass, iphone's inbuilt app could display the coordinates in a matter of seconds after calibration.
My following code, which is written in swift, finds the address based on the co-ordinates from GPS. I'm just confused why this code isn't able to display latitude and longitude in the absence of internet connectivity.
Any help is greatly appreciated. Thank you!
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)"
}