FCCurrentLocationGeocoder
iOS Geocoder on top of LocationManager and CLGeocoder for forward geocode and reverse geocode user's current location using a block-based syntax.
It can also be used to geocode the user's approximate location (always country, almost always city) without asking for permission (using a free GeoIP service).
Requirements & Dependecies
- iOS >= 5.0
- ARC enabled
- CoreLocation Framework
- FCIPAddressGeocoder
Installation
CocoaPods:
pod 'FCCurrentLocationGeocoder'
Manual install:
- Copy
FCCurrentLocationGeocoder.h
andFCCurrentLocationGeocoder.m
to your project - Manual install FCIPAddressGeocoder
Usage
iOS 8
Since iOS 8 it is required to add NSLocationWhenInUseUsageDescription
key to your Info.plist
file. Value for this key will be a description of UIAlertView presented to user while asking for location permission. See Apple documentation for more info.
Basically all you need to do is to add single entry in your Info.plist
file. Add key NSLocationWhenInUseUsageDescription
, and select type String
. The value you enter for this entry will be shown as text in UIAlertView presented to user first time you try to determine his location.
In the end it should look similar to this:
Code sample
//you can use the shared instance
[FCCurrentLocationGeocoder sharedGeocoder];
//you can also use as many shared instances as you need
[FCCurrentLocationGeocoder sharedGeocoderForKey:@"yourKey"];
//or create a new geocoder and set options
FCCurrentLocationGeocoder *geocoder = [FCCurrentLocationGeocoder new];
geocoder.canPromptForAuthorization = NO; //(optional, default value is YES)
geocoder.canUseIPAddressAsFallback = YES; //(optional, default value is NO. very useful if you need just the approximate user location, such as current country, without asking for permission)
geocoder.timeFilter = 30; //(cache duration, optional, default value is 5 seconds)
geocoder.timeoutErrorDelay = 10; //(optional, default value is 15 seconds)
//check if location services are enabled and the current app is authorized or could be authorized
[geocoder canGeocode]; //returns YES or NO
//current-location forward-geocoding
[geocoder geocode:^(BOOL success) {
if(success)
{
//you can access the current location using 'geocoder.location'
}
else {
//you can debug what's going wrong using: 'geocoder.error'
}
}];
//current-location reverse-geocoding
[geocoder reverseGeocode:^(BOOL success) {
if(success)
{
//you can access the current location using 'geocoder.location'
//you can access the current location placemarks using 'geocoder.locationPlacemarks'
//you can access the current location first-placemark using 'geocoder.locationPlacemark'
//you can access the current location country using 'geocoder.locationCountry'
//you can access the current location country-code using 'geocoder.locationCountryCode'
//you can access the current location city using 'geocoder.locationCity'
//you can access the current location zip-code using 'geocoder.locationZipCode'
//you can access the current location address using 'geocoder.locationAddress'
}
else {
//you can debug what's going wrong using: 'geocoder.error'
}
}];
//check if geocoding
[geocoder isGeocoding]; //returns YES or NO
//cancel geocoding
[geocoder cancelGeocode];
License
Released under MIT License.