[MUSIC PLAYING]
[WHOOSH]
[CLICK]
[WHOOSH]
[CLICK]
Hi, I'm Mano Marks.
And welcome to another Google Maps Developer Shortcut.
Today, we're going to be talking about earthquakes.
Earthquakes are something that happens constantly around the
world, obviously.
And the US Geological Survey gives out a JSON feed that
allows you to parse that data and put it into your
applications so that you can get notifications of
earthquakes as they're happening around the world.
So I'm going to take you, first, through a demo of this
application that I built, quick app that I built using
Storyboards and NSJSONSerialization, and then
walk you through a little bit of the code.
So this is my app, here, in the iOS Simulator.
You'll see here that I have a Table View.
And I click on the Table View, and it loads up the Google
Maps SDK for iOS and shows you the location.
And if you click on the marker, it'll actually show
you the title, which is the location in plain text.
And there's a little snippet under there
that says the magnitude.
All right, so what does this look like in code?
Well, first, let's take a look at the JSON feeds.
This is what it looks like in the browser.
So this is just a data serialization format.
But that's not really easy to look at.
So let's just take a look at what it looks
like in Sublime Text.
So you see here, each line is an object in JSON.
Now, JSON is a format that's very familiar to JavaScript
developers, and is becoming a common data format for people
working in mobile applications, as well, because
it's nice and compact and easy to parse.
And a lot of operating systems, including Android and
iOS, have developed native libraries for parsing it out.
So basically, you're seeing here a dictionary of objects.
This key-value pair is very easy to sort through.
So let's just take a look here in my code.
I defined an NSURL for the URL for the feed.
So that's this earthquakesusgsds.gov.
I'm choosing All Earthquakes in the Last Hour.
And then, when the view does load, it fetches the data.
So there's a fetchData method here.
And I create two arrays, quakeArray and markerArray.
And then, I do a dispatch_async, which is just
a way to fetch data from a URL.
And I assign it to a queue and then say, create this NSData
object at the URL that I've specified.
And then, when you're done, go and run
the fetchedData method.
And so, in here, I've got fetchedData method, which I've
passed in NSData.
And so what I do is I make this quakeArray.
It initializes it with an initial capacity of 100,
because I figure, hopefully, not over 100 earthquakes in
the last hour all around the planet.
And then, here's the key piece here.
I created a JSON object using NSJSONSerialization.
And then, I just say, JSONObjectWithObect and
responseData, which is the data that I
passed in to the method.
And that returns to me an object that I can then just
parse through, looking for keys.
It's a dictionary, basically, that I can then parse through
and create other dictionaries out of the different objects,
and then use that data in my application.
So here you see, I'm just going through the latest
Earthquakes object, which is in this array, and then
creating dictionaries or parsing out strings for each
of the different objects.
And then, what I do is I then assign the array to the
tableView as the data source.
And I reload the table.
And then, because I'm using a Storyboard--
and you can see my last Shortcut on doing
storyboards--
I prepare for the Segue by basically passing in an object
that then gets passed to the next view controller, which is
EQViewController.
And that then parses out the GMSCameraPosition from the
object and creates a marker at that location.
So you can see that working with JSON in Objective-C is
actually a very simple process.
The key thing here is NSJSONSerialization, which
provides you with some native support.
And that allows you to use web services in your application
and publish them like I did.
So that's all I've got for today.
Thank you for coming to our Shortcut.
And we'll see you next week.
[WHOOSH]
[CLICK]
[WHOOSH]
[CLICK]
[MUSIC PLAYING]