Not able to get subLocality using CLPlacemark in swift

I'm actually building an application in swift. The problem is that the application should get the sublocality from a longitude latitude and check if that subLocality is part of an array but I'm not able to get the sublocality

Can any of the professional here help me out

This is the longitude Latitude used

let loc: CLLocation = CLLocation(latitude:43.60089111328125, longitude: 3.875329601741884)

ceo.reverseGeocodeLocation(loc, completionHandler: {(placemarks, error) in
                        if (error != nil)
                        {
                            print("reverse geodcode fail: \(error!.localizedDescription)")
                        }
                        let pm = placemarks! as [CLPlacemark]

                        if pm.count > 0 {
                            let pm = placemarks![0]
                            addressString = ""

                            if pm.name != nil {
                                addressString += pm.name! + ", "
                            }

                            if pm.isoCountryCode != nil {
                                addressString += pm.isoCountryCode! + ", "
                            }

                            if pm.country != nil {
                                addressString += pm.country! + ", "
                            }

                            if pm.postalCode != nil {
                                addressString += pm.postalCode! + ", "
                            }

                            if pm.administrativeArea != nil {
                                addressString += pm.administrativeArea! + ", "
                            }
                            if pm.subAdministrativeArea != nil {
                                addressString += pm.subAdministrativeArea! + ", "
                            }

                            if pm.locality != nil {
                                addressString += pm.locality! + ", "
                            }

                            if pm.subLocality != nil {
                                addressString += pm.subLocality! + ", "
                            }

                            if pm.thoroughfare != nil {
                                addressString += pm.thoroughfare! + ", "
                            }

                            if pm.subThoroughfare != nil {
                                addressString += pm.subThoroughfare! + ", "
                            }
                            if pm.areasOfInterest != nil {
                                print(pm.areasOfInterest!)
                            }

     print(pm.addressDictionary)
}
})

Which should return GAMBETTA as subLocality but nothing is coming.

Upvotes: -1

Views: 436

Answers (2)

Manoj Rlogical
Manoj Rlogical

Reputation: 239

Can you please try, it will help you.

   let newlocation: CLLocation = CLLocation(latitude:43.60089111328125, longitude: 3.875329601741884)


        let geoCoder = CLGeocoder()
        geoCoder.reverseGeocodeLocation(newlocation, completionHandler: { (placemarks, error) -> Void in

            // Place details
            var placeMark: CLPlacemark!
            placeMark = placemarks?[0]

            print(placeMark.subLocality as Any)

            // Address dictionary
            print(placeMark.addressDictionary as Any)

            //City
            if let city = placeMark.addressDictionary!["City"] as? String {
                print("city : \(city)")
            }

     })

Upvotes: 0

axel
axel

Reputation: 422

You can use the Geocoder for reverse geocoding based on the location coordinates which will return a CLPlacemark and then you can check for the subLocality if exists like the following:

let geoCoder = CLGeocoder()
geoCoder.reverseGeocodeLocation(loc, completionHandler: { placemarks, error in
    // Place details
    guard let placeMark = placemarks?.first else { return }

    // SubLocality
    if let subLocality = placeMark.subLocality {
      // Do something if exists
    }
})

Upvotes: 0

Related Questions