Important note if your project doesn’t use ARC: you must add the -fobjc-arc
compiler flag to SVGeocoder.m
and SVPlacemark.m
in Target Settings > Build Phases > Compile Sources.
This implementation let you set the region optional parameter of the Google Geocoding API.
This implementaton uses GMSCoordinateBounds instead of MKCoordinateRegion thus allowing you to throw Apple’s MapKit away.
Note, that it uses rather rough approximation of CLRegion (conversion coordinate+radius to four coordinates).
As far as original SVGeocoder is iOS 5.0+ compatible, Google Maps SDK for iOS (1.2.0 for the moment) is 5.1+ compatible.
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 blocked-based, uses NSURLConnection
, ARC, as well as NSJSONSerialization
to automatically parse JSON responses (making it only compatible with iOS 5+). 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, Google Maps SDK for iOS (1.2.0+) and frameworks to your project.
In it’s simplest form, geocoding an address is as simple as:
[SVGeocoder geocode:addressField.text completion:^(NSArray *placemarks, NSHTTPURLResponse *urlResponse, 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
parameter to fine-tune your search:
+ (SVGeocoder*)geocode:(NSString *)address region:(CLRegion *)region completion:(SVGeocoderCompletionHandler)block;
[SVGeocoder reverseGeocode:CLLocationCoordinate2DMake(45.53264, -73.60518) completion:^(NSArray *placemarks, NSHTTPURLResponse *urlResponse, NSError *error) { // do something with placemarks, handle errors }];
SVPlacemark is now a simple NSObject. Here’s a sample placemark returned for string “3456 Saint-Denis, Montreal”:
{ ISOcountryCode = CA; administrativeArea = Quebec; coordinate = "45.517363, -73.568376"; country = Canada; formattedAddress = "3456 Rue Saint-Denis, Montreal, QC H2X 3L1, Canada"; locality = Montreal; postalCode = "H2X 3L1"; subAdministrativeArea = "Communaut\U00e9-Urbaine-de-Montr\U00e9al"; subLocality = "Le Plateau-Mont-Royal"; subThoroughfare = 3456; thoroughfare = "Rue Saint-Denis"; }
There’s also a region
(GMSCoordinateBounds
) property in case the returned placemark isn’t a pinned location, and a location
(CLLocation
) convenience property.
Orginal 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.