Home > database >  How to get CLPlacemark from Location in SwiftLocation 5.1.0?
How to get CLPlacemark from Location in SwiftLocation 5.1.0?

Time:04-08

This is a library that doesn't have a very great documentation, In the past, I had problems migrating from a previous version to a modern version, and now, I have exactly the same problem again. In SwiftLocation 4 this is the way to get the placemark from a location, passing it the coordinates of that location:

SwiftLocation.LocationManager.shared.locateFromCoordinates(location.coordinate) { result in
    switch result {
    case .success(let places):
        guard let receivedPlacemark = places.first?.placemark else {
            return
        }
        logger.debug("Retrieved placemark: (receivedPlacemark.locality ?? "-")")
        self?.currentPlacemark = receivedPlacemark

        NotificationCenter.default.post(name: Constants.Notifications.placemarkUpdateNotification, object: nil)
    case .failure(let error):
        logger.error(error.localizedDescription)
        NotificationCenter.default.post(name: Constants.Notifications.placemarkUpdateNotification, object: nil)
    }
}

Now, after upgrading to 5.1.0 version of SwiftLocation I simply can't find in the github or the cocoapods documentation of the library how to do the same with the actual version. SwiftLocation.LocationManager.shared.locateFromCoordinates doesn't exists and can't find anything similar.

Does anyone know how to do that with 5.1.0?

Thanks

CodePudding user response:

The process name for what you are trying to achieve is Geocoding.

From what I can see the location API changed a bit, so the code you are looking for is (from documentation):

let service = // insert your service here
SwiftLocation.geocodeWith(service).then { result in
    // You will get an array of suggested [GeoLocation] objects with data and coordinates
    print(result.data)
}

where service should equal to your service of choosing, but if we want to use Apple engine to get data for given coordinates, you can try:

let service = Geocoder.Apple(coordinates: location) //location must be CLLocationCoordinate2D
  • Related