DJGeocoder is a simple way to get geocoding done on iOS.
It's the process of taking an address, and finding out what latitude and longitude it corresponds to.
If you have an app that need to do this, it takes a surprising amount of code. Not so much that you can't write it, but enough to make realize that you'd rather be writing code for other things.
-
Copy the
DJGeocoder.h
andDJGeocoder.m
into your project.#import "DJGeocoder.h"
-
In your code, make yourself a Geocoder, fill in some details, and fire off a request
DJGeocoder *coder = [[DJGeocoder alloc] init]; // don't forget your memory management rules :) coder.delegate = self; coder.streetAddress = @"1 Infinite Loop, Cupertino, CA"; [coder startAsynchronousRequest];
-
Implement the delegate methods. One of them will get called when something interesting happens. (DJGeocoder finds the latitude/longitude or fails.)
- (void)geocoder:(DJGeocoder *)coder didSucceedWithLatitude:(NSNumber *)latitude Longitude:(NSNumber *)longitude Details:(NSDictionary *)details { // your awesome code here [coder release]; } - (void)geocoder:(DJGeocoder *)coder didFailWithError:(NSError *)error { // Oh No :( [coder release]; }
NO! Performing network requests on the main UI thread is a sin.
DJGeocoder uses the Google Maps Web Services API. This is a good time to mention that you should adhere to Google's terms of service when using this. In a nutshell:
[...] the Geocoding API may only be used in conjunction with a Google map; geocoding results without displaying them on a map is prohibited.
This isn't really done. There's a lot of work to do. It's quite useable, however. If you find anything wrong, submit an issue. Better yet, fork me and fix it! :)
This code was written before iOS 5.0 was introduced. If you're targeting iOS 5.0 or later, then just use CLGeocoder.