2

I am not able to get state & zip code with CLGeocoder.I get location name,country & city only.I am using this code in india for getting location.I am using below code for this.

func getAddressFromLocation(location: CLLocation) {

        CLGeocoder().reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
            print(location)
            self.activityIndicator.stopAnimating()
            if error != nil {
                DILog.print(items: "Reverse geocoder failed with error" + (error.debugDescription))
                return
            }

            var placeMark: CLPlacemark!
            placeMark = placemarks?[0]

            if placeMark == nil {
                return
            }

            // Location name
            if let locationName = placeMark.addressDictionary!["Name"] as? String {
                self.currentLocation = self.currentLocation + locationName
            }

            // Street address
            if let street = placeMark.addressDictionary!["Thoroughfare"] as? String {
                self.currentLocation = self.currentLocation + ", " + street
                self.spotLocation.street_address_1 = street

            }

            // City
            if let city = placeMark.addressDictionary!["City"] as? String {
                print(city, terminator: "")
                self.currentLocation = self.currentLocation + ", " + city
                self.spotLocation.city = city
            }

            // Country
            if let country = placeMark.addressDictionary!["Country"] as? String {
                print(country, terminator: "")
                self.currentLocation = self.currentLocation + ", " + country
                self.spotLocation.country = country

            }
            // Street address
            if let street1 = placeMark.addressDictionary!["SubThoroughfare"] as? String {
                self.currentLocation = self.currentLocation + ", " + street1
                self.spotLocation.street_address_1 = street1


            }
            // state
            if let state = placeMark.addressDictionary!["State"] as? String {
                self.currentLocation = self.currentLocation + ", " + state
                self.spotLocation.state = state
            }
            // Zip
            if let zip = placeMark.addressDictionary!["Zip"] as? String {
                self.currentLocation = self.currentLocation + ", " + zip
                self.spotLocation.state = zip
            }
            self.locationLabel.text? = self.currentLocation


        })

    }

Please tell me how can i get the solution for it ?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
TechChain
  • 8,404
  • 29
  • 103
  • 228
  • Did you `print` the `addressDictionary` to see what it contains? Why do you declare `placeMark` as implicit unwrapped optional? If the *optional(!)* array `placemarks` is not `nil` and it contains an item, the item is definetely **non-optional**. However if the array is empty your code will crash. – vadian Jun 18 '17 at 17:30
  • i have added check if placemark is nil then return – TechChain Jun 18 '17 at 17:32
  • `placemark` will **never** be `nil` if `placemarks?[0]` succeeds. However the code crashes in the `placemarks?[0]` line if the array is empty. The check for `nil` (and the IUO declaration) is meaningless. – vadian Jun 18 '17 at 17:40
  • i will add check there.Thanks for your important review.Now can you tell me can i get zipcode & state – TechChain Jun 18 '17 at 17:44
  • Once again: *Did you `print` the `addressDictionary` to see what it contains*. Does it contain the `State` and `Zip` key? And if yes, what types are the values? – vadian Jun 18 '17 at 17:46

1 Answers1

0
let zipCode = placeMark.postalCode ?? "unknown"
quemeful
  • 9,542
  • 4
  • 60
  • 69