Join Bill Weinman for an in-depth discussion in this video Getting GPS course, part of iOS 6: iOS System Resources.
- View Offline
If you are using GPS for your location services, core location is also capable of providing course and speed data when there is enough data available to derive it. Here in Xcode I have my working copy of the location project from the last lesson open. If you like, you can use a working copy of the location dash done. From the chapter five folder in the exercise files to follow along. When the data is available, the course and speed are available as properties of the cl location object. So we're going to add a couple of things, first to our local interface. First one is going to be cl location direction, cl location direction like that, and we're going to call that course.
And you notice again that it doesn't have an asterisk here. It's not a pointer to an object CLLocation direction and CLLocation speed are actually just type defs of double. So this time we're going to select CLLocation speed, we'll call this one speed. The course is in degrees, with zero as true north, and speed is in meters per second, so now we're going to fill in those values. We're going to come down here to our delegate method. Location manager did update locations. And right here, inside of this if, we're just going to add course equals manager.
That's the CL location manager. And the location object is actually a property of the manager and course; and speed equals manager.location.speed. Like that. Now that we're setting it, when we press the Stop button, we want to unset it. So, I'm going to come in here, before have coordinates, and I'm going to say course equals speed equals minus one. For both of these values, minus one is considered invalid, so I'm just going to say invalid value, just so I know what that is when I come back and look at it years from now.
And then finally, you want to display the course and speed. So we're going to come down here to our displayLocation method, and this is where we're going to do the most work. We're going to start by creating a couple of strings, so NSString. We'll call this one courseString, and set it to the empty string value. I'm going to make a Copy and Paste of this and make the next one the speed string. And then we're going to come down here and set those strings. And I say if course is greater than or equal to zero, because, remember, negative values are invalid, then the courseString equal to NSString stringWithFormat.
And inside the quotes there I'm going to put a new line and the word course and percent dot 2f. That will give us a floating point number with two decimal points of precision and the course variable. I'm going to do the same thing. I'm going to copy and paste this. Did I not put a semicolon in? I didn't put a semicolon in. Copy and paste that and say speed and it's going to be the speed string. And here where it says course, I'm going to make that comma space speed, and make this speed here.
So now I'm setting both the course string and the speed string to their respective values. We're just going to look at the raw values. We're not going to convert the meters per seconds and the miles per hour or anything like that. That's easy to do. And you probably already know how to do that, or you can easily look it up. Now here in our message line, I want to put in both of these new strings. And I'm just going to say percent at, percent at for both of them. And come down here and say, courseString and speedString. Now, our display location method is updated so that it displays both of these values.
And the values are being populated down here, in our delegate method. And we should be ready to test it. So I'm going to press Shift Cmd K to clean, Cmd B to build, I like to do all these things in separate steps the first time I build something. In case I get errors, it makes it a little easier. And Cmd R to run this in the simulator and there's the simulator and I'll press the start button here and there's our course end speed. Looks like our speed is a walking speed. What do we have here? Location, it's the city run.
If I make this the freeway drive, you watch, the speed will start speeding up, I selected -- oh yeah, there it is, speeding up, four, six, seven, nine. See, it's actually getting on an on ramp. I've watched this on the map, and in our next movie, we're actually going to bring up the map and you'll be able to see this. And, there, it's getting up to speed: 23, 30-something. When it gets up to 30-something, that's about 100 kilometers per hour or about 60, 70 miles per hour. So there we are.
So I'm going to press stop, and we'll come back here to our XCode and press Cmd period to finish running it on the simulator. The GPS course and speed values can be useful when creating location aware apps for movement like driving, running or biking. In the next movie, I'll show you how to show a map with the current location
- Preparing your app for internationalization
- Localizing strings and images
- Taking photos or video with the camera
- Reading records from the Address Book
- Registering a custom URL scheme
- Getting a device's location
- Reading raw motion data
- Posting to a specific social media outlet
- Sending messages with the Message UI framework