Not so long ago I wrote an Introduction to MapKit and at the end of that article I was suggesting, as an improvement, the ability to obtain real location data from the web instead of hard-cording locations. In this article I want to introduce you to geocoding as a way to obtain information about geographical locations. Geocoding can be forward, when we obtain the geographical coordinates (latitude and longitude) from other location data such as postal addresses, or reverse, when we obtain the address of a location by having the latitude and longitude as inputs.
In this article, I want to show how to do geocoding using both Apple Maps and Google Maps. The major difference between the two is that for Apple Maps you do not need to access an external URL to get location data, while for Google Maps you do. I will start with Google Maps geocoding, so let’s create a new Xcode project first. In ViewController add two properties, one to hold the base URL for the Google Maps API, and a second one for your API key.
Next, let’s see how forward geocoding is done using Google Maps. For that let’s create a new method:
This method looks pretty straightforward. First it constructs a URL using the base URL, a zip code (provided as input argument), and the API key, then it serializes the data it finds at this URL into JSON feed, and finally it parses the JSON response to obtain the latitude and longitude we were looking for. Now let’s call this method in viewDidLoad():
If you run the project now, you will see the following output in the console:
It worked! Next, let’s write a method for reverse geocoding using Google Maps:
This method is pretty similar to the first one. The only thing we changed is we provided the latitude and longitude as inputs to get the address information. Let’s call this method in viewDidLoad():
Run the project and you should see the following output at the console:
This is great! We were able to get the exact location of Apple’s headquarters just by using the latitude and longitude. Ok, it’s time to move on to using Apple Maps now and see how that works. First, let’s import what we need:
Then, let’s write a method for forward geocoding using Apple Maps:
Let’s look at what this method does. First, it uses the CLGeocoder class from the CoreLocation framework to geocode an address we provide as input. In the method’s completion handler we parse the response and see what placemark objects we found. We are particularly interested in the first one found, if there are more than one. Then we get the location coordinates and any areas of interest - as properties of the placemark. Now let’s call this method in viewDidLoad():
If you run the app now, you will see the data you would expect:
Now let’s write a method for reverse geocoding using Apple Maps:
The only detail we need to pay attention to in this method is the ABCreateStringWithAddressDictionary class from the AddressBookUI framework. This class does the address parsing for us which is really convenient as we do not need to care for any of the location fields, if we just want to see the entire address. Let’s also call this method in viewDidLoad():
Run the app and you will see this nicely formatted output in the console:
You can read more about Apple Geocoding as well as Google Geocoding. As a next step, you could take this information and place pins on a map view, for example, and show areas of interest. You could also implement a search feature for finding addresses while you’re typing. You could also implement an autocompletion feature while at it. There are many opportunities you could think of.
Until next time!