[MUSIC] As we talk about using location and in particular, as we talk about geo-regions, geo-fences and regions. A little bit about iBeacons, we're going to need to talk a little bit more specifically about simulating location. In previous lectures in this specialization, what we've seen is examples of using the map view. And using some of the built in location simulation that's present in the simulator. There's actually some ability to do some more fined grained simulation of location. And we're going to do that, so we have a good way of testing our regions, when we go about implementing them. So this lecture is about building a simulated location track and then, what we're going to do. Is in our case study, we're going to leverage it and show you how to put it into the simulator at that point. So the high level idea, is that as an app developer, you need to carefully simulate movements to test things. Like geofences and the question is how do you do that? Well, what you do is you build a waypoint file and the format of the waypoint file is something called a GPX file. It's sort of a file that's leftover from GPS logging devices that predated cellphones. It is in XML format, if you're familiar with XML, means it's got a lot of tags. It looks vaguely like HTML, except that instead of having tags like body and had and anchor and image. You have other tags that describe a progression of movement through time and space. So we're going to build one of those. What we wan tto do is, we want to put several geographic points into that file. Those geographic points are called waypoints and basically, they're turning points. You go straight until you hit a weigh point and then you're assumed to go straight until you hit the next weight point. And the times at which you hit each weigh point are specified through time stamps. And it's assumed that you're moving evenly and regularly between each weigh point in a straight line. Together those waypoint describe a path. And that path becomes the simulated route that the device takes as your running your application. It's treated as a circle, so when you get to the end the device has assumed to go back to the beginning and restart it. According to the plan that you've indicated, so that can be used to simulate going through different regions. But what it could be really used for is testing to make sure that your device is doing what you expect it to do. So you are going to control the simulator's location, meaning the simulator's simulated location. You're going to control it through Xcode's window, instead of through the simulator window. So, when there are a couple facilities for doing this in the simulator as well. And we're going to bypass that by specifying this track in the Xcode window. We'll look at that in the case study in more detail. So here's an example of the GPX format. I just want to kind of point out a few things and then show you how we're going to go about building it. First of all, it's important that you get the syntax exactly right. And if you want to make sure that you get it right, you can do a web search for xml online lint, l-i-n-t. And what that'll do is, that will bring up some online tools which you can cut and paste your xml into. And validate that you have the correct format, if it's not the right format action, code is not going to be able to read it. So, what you can see in this file is that you start with a line that describes the XML and that's not specific to the GPX format. That just says that this is an XML file. And then you see a series of nested items, the top level item is a tag that says GPX. So, you can see that's at the top and that's at the bottom. And there's some attributes in that GPX tag, for example, xml and ask which stands for xml name space. And then you have xmlns:xsi, that's an xsi attribute in the xml name space. These describe the legal language that you can use to write out the different waypoints. So those first two lines are boilerplate, they should be the same in all GPX files that we use in Xcode. And then perhaps with the exception of creator, you can put your own name in there or use the website that you created it with. The important plot point is a sequence of wave points described with WTP tags. And each one of those wave points has a latitude and longitude listed as an attribute for the tag and then embedded within the tag. Or nested within the tag is a timestamp, which describes at what point the device is supposed to hit that particular location. All right, so that's the file that we're working with, I'll show you how to get it started. During simulation the day doesn't matter, what matters primarily is the relative time between points. So, the assumption is that you're starting at the first day and time at the beginning of that week point. And then, you proceed to the next one as if the clock starts at that first time stamp. Actually, in all the way points that I've done, I've always had the same day, so I'm not sure if Xcode will respect multiple days. Because I can't imagine people simulating multiple days in the simulator. I've always had the day be the same and change the times. But you can see that in the example on the left, maybe it's too small now. We start at midnight, not because we really care about simulating midnight but because that's a good starting point. And the subsequent points are at thirty seconds later, a minute thirty, two minutes, and three minutes later. So you get this progression through those points. The relative time points are the only thing that I ever pay attention to. What you can do is to use an online site to assist you in building this skeleton. So the one on the left I started with, I'm using this website http://gpx-poi.com. GPX being the format of the file and POI standing for point of interest. Another thing that you could do is, you could use Google Earth to describe a path. Export the file in KML, which is Google's preferred version of the GPX file. But Xcode doesn't recognize KML, so you'll need to convert the KML to GPX. Again, using some online tool that's available, somewhere, so it's a multi step process, if you want to do it. I tend to do it, kind of manually, so let's take a look at that. So, if you go to that website, gpx.poi and again, there's nothing special about this particular website. It just happens to be one that will help you find particular points and start that template. And so this is what it looks like, and you can see that there is a point on the map that's specified in the middle of Africa. I think that's 0000 for latitude and longitude. And the first thing that you do is, you want to find the first point at which you want your track to start. So I'm going to try and drill down into Santa Barbara and try and create a track that walks across the Westmont College campus. Where I am, where I'm teaching right now. So you have to drill down really far, because this is a very specific path that you want to follow. Helps me to be on satellite because I can see some sports fields that we can walk around. And then as you get lower, you sometimes have to move the target because it was inaccurate and you're at a higher level. And eventually, when you drill down and find the place that you want and you settle on the position of that marker. You can move ahead to create your initial template, all right, so here I am finding the sports field. I want to walk out the parking lot by the sports field and I'm going to have the track end at the walk across campus. There we go, I drop the point at the end of the parking lot and now there is two steps in this website to get that. You have to hit update and that moves the point from the left to the right. Then you come down to the bottom, you hit create, and that's going to update your template here. If you copy and paste that into a text file, initially that's what I did, that becomes the starting for your track. Now, notice that this only has one particular wait point in it, that point that we picked at the top. What we need to do is we need to add multiple wait points in order to create a track that you'd follow. It's important, if you're using this particular website, be careful that you hit update and then create each time you create a new point. Because if you don't do that, you'll get data that's left over from the last time you used it. For example, just hit create, and it will move your point from the map to the form but it won't update the text that I just highlighted. All right, so after you do that, you can grab another point, all right, so now I moved the point to the end of the parking lot. Drop it off there, now I'm goning to hit Update to move that point from the map over to the form, once I'm confident with the spot. And it's right outside our lab, update moves it to the left, we've got new points up there, now we have to come down. We have to hit Create again in order to get New template, all we need is that additional weight point. So you don't have to get the whole thing, but just to demonstrate what's happening, I'm going to go ahead and grab the whole thing. The next thing that I do is, I'm going to put all that into a text file. So in this text file, what we see here is, we see all of the different weight points that I grabbed. I put all of the text in there And what I'm going to do is, I'm going to just keep that top bit of XML as my structure. And out of all the other ones I'm just going to pull out the waypoint tags. You can see that I'm cutting out everything except the waypoint tags. I'm going to copy that and move it up to the top, cut it, move it up to the top. Go down, get my next waypoint that I graphed as I created this track across campus. Grab just the way point piece out of that, add that to the template above, get the next waypoint, good, cut that one. Move that to the top and once you get all of them in place, you can put it into an online XML checker. Ro make sure you didn't cut anything you shouldn't have cut, any characters or anything. And also you can use it to, you can format it. I have a tool on my computer that will format it for me in the editor, so I'm going to go ahead and format it here. And once it's formatted we will have all, what do we have, one, two, three, four, five, six different wave points. The next thing that you need to go do is, you need to go through and you need to clean up all the time stamps. So, the first thing that I'm going to do is, I'm going to find all the time stamps. And I'm just going to write this regular expression here, to highlight what we're editing. I'm going to go through and I'm goin gto set them all initially to zero. So, it's as if you should be at each one of those waypoints at exactly the same time. And then, I'm going to add a reasonable amount of time through each of those waypoints. Now what's a reasonable amount of time? Well, you have to be familiar with the area, and you have to understand the scale at which your points are being picked. And then you have to think about what kind of movement are you trying to simulate. Walking or a car or a bike or a bus or something like that, then you've to pick times that are appropriate. You can use some rule of thumb in terms of distance, for how long you want to use for your simulated test. And then In this case, I'm just going to assume a rapid walk, almost a run across campus. Because I don't want to spend too much time with it. So I start the first one at zero zero, add 30 seconds to the next one, add 30 more seconds. So that it's 1 minute at the third way point, at the forth way point 30, then 2 minutes then 2 minutes 30. And the assumption that Xcode uses is that when you're done with that last way point. You're going to move back to the beginning again and the clock will restart, so a good practice is to have some kind of a circular route. Or assume it's a circular route, so you don't have a jumping behavior at the end of your task line. Going back to the beginning. Okay so in summary, it's really important to simulate location to test location based apps. You want a high-quality, controlled simulation, this high-quality testing requires specific locations. And the way Xcode does this is with .gpx files. So, in this lecture, what we did is, we showed you how to create a .gpx file. The only thing we didn't show you is, how to validate that your syntax is right. But you do that using xml online lint tool and what we're going to do next is, in our case study. We're going to leverage this simulated location, in order to test our geo fenced regions. Okay, thank you. [MUSIC]