Maps Shortcuts: Directions and the Google Maps iOS SDK


MANO MARKS: Hi, I’m
Mano Marks. I’m a Developer Advocate on
the Google Maps Developer Relations team. I’m going to talk to you today
about putting driving directions in your Google Maps
iOS app using the Google Maps SDK for iOS and also the
Google Geocoding API. So you’ll take a look
here at my screen. You’ll see that what I’ve got
is an iPad simulator. And I’m just going to click
on two different points. And you’ll see that
it drew a polyline between the two of them. So what we’re going to do is
walk through that demo. Now before I do that, I just
want to give you a pointer to all the things that this
is going to depend on. We are going to be getting
an NSData object from dataWithContentsOfURL. So this is a standard way
of pulling in JSON. And we’re going to do
that to pull in the Google Geocoding API. We’re going to get back an
NSJSONSerialization object, which we are then going
to walk through pulling out data from. Another piece of this that
we’re going to use is mapView:didTapAtCoordinate. This is just a way to trap the
events for when you click, which is what you saw
me do in the demo. And we’re going to use
GMSPolylineWithPath. We’re going to do this
all very quickly. So if you’ve seen before the
shortcut that I did on geocoding and also the one I did
on an earthquake tracker, some of this is going to
be familiar to you. And you’ll see how when I
walk through the code. So what I’ve got here
is a standards app. I prefixed everything MD
for Maps Directions. I had a little storyboard for
putting up my application. And then you’ll see I’ve got an
MD view controller and MD direction service. So I farmed out to a separate
class the MD direction service for trapping in the directions
just to make that code a little bit more reusable. And by the way, any code that I
show you today I’m going to put up in GitHub under
my personal account. So here we’re in the
view controller. And you can see what I did here
is I created a map view, a dib map at coordinate. And what that does is it takes a
coordinate that results from the tap on the map. And from there, you can
pull out a position. And then I created a marker
based on that. So the position is just a
CLLocation coordinate, 2D. And I add that marker to
the waypoints array. And I create a position string
from each coordinate. So as I tap it, coordinate,
latitude coordinate, longitude, I just create a
string that I then store in the waypoint strings because
that’s going to be easier for constructing a Google
geocoding URL. Then I check and see if the
count of the number of objects in the waypoint string array
is greater than one. And if it is, I then pass in
some parameters to the geocoding request. So you see here, I’m
creating a sensor. And I’m just going to default
this to be false because the position that’s being detected
is not being detected from a sensor. And just to point out, most of
you know, sensor in this case doesn’t actually give you
any additional benefits. It’s just our way of tracking
whether or not you’re using a sensor so we can report that
to our data providers. Then if you look here– I’m creating a parameters array
in which I created with objects sensor and the waypoint
strings, so all the strings that have been created
up to that point. And then I create a keys object,
a keys array rather. Array with objects sensor and
waypoints being the two keys. And I pass that as a dictionary
to an object called query to MD directions
service. I also passed to it a selector
add directions, which is going to take care of adding the
directions to the map once I’m done getting the directions. And I add pass in the
delegate self. This is a standard messaging
model in iOS. So what we’ve got here then is
I’m creating the service. And you see here I’ve got the
set directions query with selector and with delegate. And by the way, the reason I’m
doing this sort of selector delegate model for messaging
is so that I can do an asynchronous call to our
geocoding services. This is a little bit different
from my last shortcut where I did a synchronous call to
the geocoding service. Just want to show you
a different way. And generally, when you’re
calling to web services, you want to do an asynchronous call
because that gives your application the opportunity to
keep going if the web service takes a while to populate. So here I create an array of
waypoints just from the query object pulling out
the waypoints. I get the origin, which
is the first one. I count the number
of waypoints. And I get the destination,
which is the last one. And then I pull out
the sensor. Then I create a URL string,
which is basically the base URL, which is this here. This is the geocoding
API’s base URL. And you notice I’ve used
the JSON version of it. You can also use an XML
based version of it. Most developers these days are
preferring to use JSON because it’s really easy to work with. OK. So then I’m basically creating
a new URL string with origin destination sensor and if the
waypoint count is more than two, additional waypoints. So you can pass to the
directions API, the ability to track multiple waypoints in
directions, that is saying pass through all these points
between the origin and the destination and then optimize
the route or not– your choice. So then I’m just adding
in additional waypoints to the string. And then here I’m basically
URL encoding the string. And then once it’s done, I
retrieve the directions with a particular selector
and delegate. So retrieve directions
is the next function. So retrieve directions I pass
in the selector and delegate and basically say, create
this data object. And then pass it to the selector
and delegate that I’ve chosen, which brings us
back to add directions. So add directions returns this
object, this JSON object. And I’m grabbing the
objectForKey routes. So if you look at the direction
response, that’s where the core– the root object. And then I’m finding the route,
which is objectForKey overview polyline. Now an overview polyline is a
way of encoding the directions into a polyline– into a string,
which compresses the string greatly over simply
creating coordinates for each point along the string. It describes it in a highly
dense format. And then GMS path, which is how
you create a path in the Maps SDK, then can read that
encoded polyline into it and create a path out of it. Then finally, I create a
polyline from that path object and put it onto the map. And that’s it. Now you can see the optimization
happens when I add in additional points
along the map. So that’s it for this
map shortcut. Thank you very much for
joining us this week. And we’ll see you next week.

6 Comments

Add a Comment

Your email address will not be published. Required fields are marked *