Im getting coordinates from , but I'd like to be able to use them outside the Location Manager Function. Is it possible to make them instance variables? If so, could someone give me some example code for that? In the code below I print within the func and it works, but I'd like to be able to, for example print outside it, and otherwise use the coordinates. Perhaps as a global variable? How would i code that? I cant quite make it out from the documentation.
import UIKit
import CoreLocation
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
var locationManager: CLLocationManager!
var seenError : Bool = false
var theSpan: MKCoordinateSpan = MKCoordinateSpanMake(0.1, 0.1)
var locationStatus : NSString = "Not Started"
var initialLoc:Int = 1
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.requestAlwaysAuthorization()
locationManager.delegate = self
var initialLoc:Int = 1
// Do any additional setup after loading the view, typically from a nib.
}
@IBOutlet weak var Map: MKMapView!
@IBAction func Here(sender: AnyObject) {
initLocationManager()
}
func initLocationManager() {
seenError = false
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.startUpdatingLocation()
Map.showsUserLocation = true
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var locationArray = locations as NSArray
var locationObj = locationArray.lastObject as CLLocation
var coord = locationObj.coordinate
if initialLoc == 1 {
var Region:MKCoordinateRegion = MKCoordinateRegionMake(coord, theSpan)
self.Map.setRegion(Region, animated:true)
initialLoc = 0
}
println(coord.latitude)
println(coord.longitude)
}
// Location Manager Delegate stuff
// If failed
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
locationManager.stopUpdatingLocation()
if (error) {
if (seenError == false) {
seenError = true
print(error)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}