SVGeocoder is a simple Cocoa wrapper for the Google Geocoding Service. It allows you to quickly geocode and reverse-geocode addresses and coordinates. It is block-based, uses NSURLConnection internally as well as JSONKit (included in package). Make sure you read the Google Geocoding Service Terms of Service before using SVGeocoder.
- Drag the
SVGeocoder/SVGeocoder
folder into your project. - Add the CoreLocation, MapKit and AddressBook frameworks to your project.
In it’s simplest form, geocoding an address is as simple as:
[SVGeocoder geocode:addressString completion:^(NSArray *placemarks, NSError *error) { // do something with placemarks, handle errors }];
Where placemarks
is an array of SVPlacemark
objects (see more about that below).
Additionally, you can use Google Geocoding API’s region
and bounds
parameters to fine-tune your search:
+ (SVGeocoder*)geocode:(NSString *)address bounds:(MKCoordinateRegion)bounds completion:(void (^)(NSArray *placemarks, NSError *error))block; + (SVGeocoder*)geocode:(NSString *)address region:(NSString *)region completion:(void (^)(NSArray *placemarks, NSError *error))block;
[SVGeocoder reverseGeocode:CLLocationCoordinate2DMake(45.53264, -73.60518) completion:^(NSArray *placemarks, NSError *error) { // do something with placemarks, handle errors }];
Make sure you cancel requests for which the user isn’t waiting on anymore by keeping a pointer to your SVGeocoder object and calling cancel
on it. In all cases, the request will time out after 20s.
SVPlacemark is basically an MKPlacemark subclass but with a coordinate
property. Here’s what an SVPlacemark object looks like:
{ address = { Country = Canada; CountryCode = CA; State = Quebec; Street = "3456 Rue Denis"; ZIP = "J0K 1S0"; }, coordinate = { latitude = "46.01529"; longitude = "-73.76997"; }, formattedAddress = "3456 Rue Saint-Denis, Montreal, QC H2L 4S9, Canada" }
SVGeocoder is brought to you by Sam Vermette and contributors to the project. If you have feature suggestions or bug reports, feel free to help out by sending pull requests or by creating new issues. If you’re using SVGeocoder in your project, attribution would be nice.