Hi, I'm Mano Marks.
I'm a developer advocate on the Google Maps Developer
Relations team.
I'm coming to you from our Sydney office, where I'm going
to be talking to you about using geocoding with the
Google Maps SDK for iOS.
Now geocoding is the art of turning an address into a
lat-long pair with some additional information.
Let me show you an example.
You see my screen here.
I'm in the iOS simulator.
I'm going to try and geocode Sydney, New South Wales,
because that's where I am.
So I click on Geocode, and it returns to me, shifts the
center of the map and returns to me the latitude and
longitude, so I can create a marker there, and actually
gives me the full formatted address as well.
Now this is done using the Google geocoding API.
So this is something that we use when we're looking at
doing geocoding on mobile devices.
It's a web service that can return either JSON or XML and
allows you to get a lot of information about it.
You can see here a standard JSON return.
So you'll see here there's lots of different
information in there.
You get address components, so you get the street number, the
street, the city, state or province, the ZIP, the
country, all that sort of thing, as well as a formatted
address and the geometry, which includes a
latitude-longitude pair.
There's some more information in here as well.
You get some viewpoints and the location type.
We're going to just concentrate on the
latitude-longitude pair.
OK, so let's take a look at the code here.
So what I've done is I've created an
object geocoding service.
Let's take a look at the header file here.
All I've got on it is init and a geocoding address
that takes a string.
And then it has, as a property, a dictionary, which
is just a geocode.
So what I do is when I initialize it, I set
geocode to 0, 0--
that's latitude and longitude zero--
and call it Null Island.
Anybody play "Myst"?
It's an old video game.
Null Island was at 0, 0.
Anyway, then in geocode address, I just create a
string, which is the base URL, which you can see here, and
pass in the address as a parameter to that string, and
then I construct a URL out of it.
It's important to do a string by adding percent escapes
using encoding.
And then I use UTF string encoding just in case it's an
address that comes from a different set of characters
than ASCII or whatever else I'm using.
So then I create an NSURL object.
And here I'm doing dispatch sync, which is synchronous
dispatch, just to make the code a little bit easier for
demo purposes.
But you probably want to do this in an async manner so
that, when you send off the geocode request, you can keep
doing other things and then come back to it.
OK, so then I create an NSData object, data with contents of
that particular URL, and I use fetchData is my callback.
Or when it fetches the data, it puts it
into this data object.
OK, so then what I do is I use NSDictionary JSON, NSJSON
serialization, JSON with object data.
So I'm just creating a serialization of the JSON as a
NSDictionary and then I can just pull out the different
sets of things.
So it's a results object.
So I pull out results object.
And then I get the individual results.
So it's possible that you'll get more than one result if
we're not 100% confident that what we're giving you is the
full and complete address.
So then I'm pulling out the first result.
And then I just pull out the address, and latitude and
longitude, using objectForKey, formatted address, then I pull
out geometry, which has a location object, which is
latitude and longitude.
It's kind of this, basically, stepping through, creating
these little dictionaries, and then strings for each of the
different values.
And then I create a dictionary object that has the latitude
and longitude and make the geocode property
equal to that object.
And then, in my service, what I did was I
have an object that--
you know, I have my View, and I have my Button,
and I have all that--
I'm not going to get into all of that at the moment.
But I just have an addMarker function that gets
called when I'm done.
So you can see it's actually conceptually pretty easy to
use a geocoding web service.
You can also apply this, then, to our directions and other
APIs as well.
So hopefully this'll get you started.
And I'm going to post this to my GitHub repository,
github.com/manomarks.
Under demos, you'll see this code.
And we'll link to that in the description of
the video as well.
So thanks a lot and happy geocoding.
Bye.