Swift - Generate an Address Format from Reverse Geocoding

前端 未结 10 1408
难免孤独
难免孤独 2021-01-31 03:46

I am trying to generate a Formatted Full address using CLGeocoder in Swift 3. I referred to this SO thread to get the code given below.

However, sometimes the app crashe

10条回答
  •  南方客
    南方客 (楼主)
    2021-01-31 04:21

    I create my own static class for Geocoding and get attributes of CLPlacemark and obtain a complete address, like "usually" returns Google:

    import Foundation
    import CoreLocation
    
    class ReverseGeocoding {
    
        static func geocode(latitude: Double, longitude: Double, completion: @escaping (CLPlacemark?, _ completeAddress: String?, Error?) -> ())  {
            CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: latitude, longitude: longitude)) { placemarks, error in
                guard let placemark = placemarks?.first, error == nil else {
                    completion(nil, nil, error)
                    return
                }
    
                let completeAddress = getCompleteAddress(placemarks)
    
                completion(placemark, completeAddress, nil)
            }
        }
    
        static private func getCompleteAddress(_ placemarks: [CLPlacemark]?) -> String {
            guard let placemarks = placemarks else {
                return ""
            }
    
            let place = placemarks as [CLPlacemark]
            if place.count > 0 {
                let place = placemarks[0]
                var addressString : String = ""
                if place.thoroughfare != nil {
                    addressString = addressString + place.thoroughfare! + ", "
                }
                if place.subThoroughfare != nil {
                    addressString = addressString + place.subThoroughfare! + ", "
                }
                if place.locality != nil {
                    addressString = addressString + place.locality! + ", "
                }
                if place.postalCode != nil {
                    addressString = addressString + place.postalCode! + ", "
                }
                if place.subAdministrativeArea != nil {
                    addressString = addressString + place.subAdministrativeArea! + ", "
                }
                if place.country != nil {
                    addressString = addressString + place.country!
                } 
    
                return addressString
            }
            return ""
        }
    }
    

    Then the implementation:

        ReverseGeocoding.geocode(coordinate: coordinate, completion: { (placeMark, completeAddress, error) in
    
            if let placeMark = placeMark, let completeAddress = completeAddress {
                print(placeMark.postalCode)
                print(placeMark)
                print(completeAddress)
            } else {
                // do something with the error
            }
    

    Finaly the print:

    15172
    Calle del Arenal, 4, Calle del Arenal, 4, 15172 Oleiros, A Coruña, España @ <+43.33190337,-8.37144380> +/- 100.00m, region CLCircularRegion (identifier:'<+43.33190337,-8.37144380> radius 70.84', center:<+43.33190337,-8.37144380>, radius:70.84m)
    Calle del Arenal, 4, Oleiros, 15172, A Coruña, España
    

提交回复
热议问题