0

I am successfully getting the current address details based on my location. It printlns perfectly. What is throwing me is how I extract the data from this call. I have tried passing, say the ZIP/Postcode, as local and even global variables but with no joy. The data only seems to exist within this call. How can I use it elsewhere?

// Get Address Information
    let geoCoder = CLGeocoder()
    let newLocation = CLLocation(latitude: valueLatitude, longitude: valueLongitude)
    geoCoder.reverseGeocodeLocation(newLocation, completionHandler: {(placemarks: [AnyObject]!, error: NSError!) in
        if error != nil {
            println("Geocode failed with error: \(error.localizedDescription)")
        }
        if placemarks.count > 0 {
            let placemark   = placemarks[0] as! CLPlacemark
            let addressDictionary = placemark.addressDictionary
            let address     = addressDictionary[kABPersonAddressStreetKey] as! NSString
            let city        = addressDictionary[kABPersonAddressCityKey] as! NSString
            let state       = addressDictionary[kABPersonAddressStateKey] as! NSString
            let postcode    = addressDictionary[kABPersonAddressZIPKey] as! NSString
            let country     = addressDictionary[kABPersonAddressCountryKey] as! NSString

            println("\(address) \(city) \(state) \(postcode) \(country)") }
    })
Edward Hasted
  • 3,201
  • 8
  • 30
  • 48
  • Where is this called ? – The Tom Jun 18 '15 at 07:58
  • It's within an IBAction. When the user presses the Save button a load of location data is saved. It executes this strip of code about halfway through. – Edward Hasted Jun 18 '15 at 12:27
  • Weird issue, you say if you assign your placemark object to a global var it doesn't work ? – The Tom Jun 18 '15 at 12:29
  • Will post up some further code in about 8 hours when home. – Edward Hasted Jun 18 '15 at 13:10
  • Hi this is the IBAction code that I am using: – Edward Hasted Jun 18 '15 at 21:44
  • We cannot see the code, could you edit your question to add the relevant code ? – The Tom Jun 19 '15 at 05:53
  • I'll have to put it up in a number of comments as it's saying its too long. I suspect the problem is that the geocoded function is executed on a remote server. – Edward Hasted Jun 19 '15 at 07:01
  • Geocoder is an asynchronous request made to Apple servers, that is why you have the completion block. Maybe you are trying to access your variables before its completion – The Tom Jun 19 '15 at 07:03
  • @IBAction func btnInsertClicked(sender: AnyObject) { var locationRecord: LocationRecord = LocationRecord() // Get Address Information let geoCoder = CLGeocoder() let newLocation = CLLocation(latitude: valueLatitude, longitude: valueLongitude) geoCoder.reverseGeocodeLocation(newLocation, completionHandler: {(placemarks: [AnyObject]!, error: NSError!) in if error != nil { println("Geocode failed with error: \(error.localizedDescription)") } – Edward Hasted Jun 19 '15 at 07:04
  • if placemarks.count > 0 { let placemark = placemarks[0] as! CLPlacemark let addressDictionary = placemark.addressDictionary let address = addressDictionary[kABPersonAddressStreetKey] as! NSString let city = addressDictionary[kABPersonAddressCityKey] as! NSString let state = addressDictionary[kABPersonAddressStateKey] as! NSString let postcode = addressDictionary[kABPersonAddressZIPKey] as! NSString – Edward Hasted Jun 19 '15 at 07:04
  • let country = addressDictionary[kABPersonAddressCountryKey] as! NSString let universalVariableString = address NSLog("Address 1 kb = %@",addressDictionary[kABPersonAddressStreetKey] as! NSString) NSLog("Address 1 var = %@",universalVariableString) println("\(address) \(city) \(state) \(postcode) \(country)") } }) NSLog("Address 2 var = %@",universalVariableString) } – Edward Hasted Jun 19 '15 at 07:05

1 Answers1

1

Your problem is most likely due to the fact that reverseGeocodeLocation is an asynchronous request made to Apple servers.

What needs to happen is:

  1. You call reverseGeocodeLocation
  2. reverseGeocodeLocation finishes, starts its completion which calls a method passing the placemark you just recovered.

In order to do that:

@IBAction func btnInsertClicked(sender: AnyObject) { 
    var locationRecord: LocationRecord = LocationRecord() 

    // Get Address Information 
    let geoCoder = CLGeocoder() 
    let newLocation = CLLocation(latitude: valueLatitude, longitude: valueLongitude) 
    geoCoder.reverseGeocodeLocation(newLocation, completionHandler:
        {(placemarks: [AnyObject]!, error: NSError!) in 
            if error != nil { 
                println("Geocode failed with error: (error.localizedDescription)") 
            }

            if placemarks.count > 0 { 
                let myPlacemark = placemarks[0] as! CLPlacemark 

                // Here call the method that uses myPlacemark
                self.myAwesomeMethod(placemarks[0] as! CLPlacemark)
            } else {
                println("No placemark")
            } 
        })
}

Where you need to use it in your code:

func myAwesomeMethod(placemark: CLPlacemark) {
    // Do stuff with placemark
}

I won't have my mac until tonight but if that doesn't work, leave a comment and we will work this out

The Tom
  • 2,790
  • 6
  • 29
  • 33