Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed location from address geocoding #189

Merged
merged 2 commits into from
Mar 16, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions Sources/GeocoderRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,32 +209,31 @@ public final class Geocoder_Apple: GeocoderRequest {

let geocoder = CLGeocoder()
self.task = geocoder

let geocodeCompletionHandler: CoreLocation.CLGeocodeCompletionHandler = { [weak self] (placemarks, error) in
guard let this = self else { return }

this.isFinished = true

if let err = error {
this.failure?(LocationError.other(err.localizedDescription))
return
}

this.success?(Place.load(placemarks: placemarks ?? []))
}

switch self.operation {
case .getLocation(let address, let region):
geocoder.geocodeAddressString(address, in: region, completionHandler: { (placemarks, error) in
self.isFinished = true
})
geocoder.geocodeAddressString(address, in: region, completionHandler: geocodeCompletionHandler)
case .getPlace(let coordinates, let locale):
let loc = CLLocation(latitude: coordinates.latitude, longitude: coordinates.longitude)

if #available(iOS 11, *) {
geocoder.reverseGeocodeLocation(loc, preferredLocale: locale, completionHandler: { (placemarks, error) in
self.isFinished = true
if let err = error {
self.failure?(LocationError.other(err.localizedDescription))
return
}
self.success?(Place.load(placemarks: placemarks ?? []))
})
geocoder.reverseGeocodeLocation(loc, preferredLocale: locale, completionHandler: geocodeCompletionHandler)
} else {
// Fallback on earlier versions
geocoder.reverseGeocodeLocation(loc, completionHandler: { (placemarks, error) in
self.isFinished = true
if let err = error {
self.failure?(LocationError.other(err.localizedDescription))
return
}
self.success?(Place.load(placemarks: placemarks ?? []))
})
geocoder.reverseGeocodeLocation(loc, completionHandler: geocodeCompletionHandler)
}
}
}
Expand Down