1 00:00:00,890 --> 00:00:04,380 All right my friends in this video we're going to write out some code to trick all of our different 2 00:00:04,380 --> 00:00:10,290 test devices be it a simulated device or a physical into thinking that the user's location is changing 3 00:00:10,320 --> 00:00:11,230 over time. 4 00:00:11,370 --> 00:00:17,520 So to get started I'm gonna go back over my code editor and inside of my S R C directory I'm going to 5 00:00:17,520 --> 00:00:25,440 make a new file called underscore mock location dot J.S. so the idea is that this is kind of like a 6 00:00:25,440 --> 00:00:29,580 test only file that's going to mark out our users location. 7 00:00:29,580 --> 00:00:33,390 We would definitely not want to include this file inside of any kind of production build. 8 00:00:33,450 --> 00:00:38,190 So over time are going to be kind of toggling an import statement to either include this file or not 9 00:00:38,190 --> 00:00:39,780 use it at all. 10 00:00:39,780 --> 00:00:39,980 All right. 11 00:00:39,990 --> 00:00:44,130 So inside of here we're going to first add in a little import statement we're going to say import star 12 00:00:44,790 --> 00:00:48,550 as location from Expo location. 13 00:00:48,750 --> 00:00:52,960 So internally at the location library exports a ton of different named functions. 14 00:00:53,040 --> 00:00:57,480 By using star as location we're essentially getting a single variable that references all those different 15 00:00:57,480 --> 00:01:03,300 functions then right after that we're gonna write out a little constant variable right here you're going 16 00:01:03,300 --> 00:01:05,450 to see why we are defining this in just a moment. 17 00:01:05,450 --> 00:01:14,310 I'm going to say 10 meters with degrees and I'm going to define this as zero point zero zero zero one. 18 00:01:14,310 --> 00:01:19,480 Like so so this represents 10 meters in longitude or latitude. 19 00:01:19,480 --> 00:01:24,190 Now technically it's not exactly 10 meters in both longitude and latitude just because the actual physical 20 00:01:24,190 --> 00:01:24,880 shape of the world. 21 00:01:25,060 --> 00:01:32,110 But it's essentially close enough after that we're going to define a function called get location. 22 00:01:32,290 --> 00:01:37,940 This is going to take a variable called increment that's essentially a counter variable so from this 23 00:01:37,940 --> 00:01:44,030 function right here we are going to return an object that essentially is a fake location reading it's 24 00:01:44,030 --> 00:01:48,190 going to fake out some fake longitude and latitude and timestamp. 25 00:01:48,200 --> 00:01:53,290 This is going to be what actually kind of tricks the expo location into thinking that our physical or 26 00:01:53,290 --> 00:01:57,240 our emulate device is moving around the world when it really isn't. 27 00:01:57,290 --> 00:02:02,420 So inside of you're going to say return an object and I'm going to give this all the properties that 28 00:02:02,450 --> 00:02:05,690 a normal location reading off a device would have. 29 00:02:05,720 --> 00:02:09,670 We haven't really looked at what these different readings are but we will see them very quickly. 30 00:02:09,740 --> 00:02:12,050 So first off I can give this thing a timestamp. 31 00:02:12,260 --> 00:02:14,530 The timestamp can be any value which just has to be a number. 32 00:02:14,540 --> 00:02:20,890 So I'm going to put in like 1 0 0 like so and then going to give it a coords property inside of here. 33 00:02:21,070 --> 00:02:33,580 I'm gonna give it a speed of 0 a heading of zero an accuracy of 5 an altitude accuracy of 5 an altitude 34 00:02:34,530 --> 00:02:40,260 of 5 and then here are the two that actually make her actually important for us we're going to do a 35 00:02:40,350 --> 00:02:55,670 longitude of negative 120 2 0 3 1 2 1 8 6 and we're going to add on increment times 10 meters with degrees 36 00:02:57,630 --> 00:03:10,110 and then 4 latitude we're gonna put in 3 7 3 3 2 3 3 1 4 1 increment times 10 meters with degrees now 37 00:03:10,270 --> 00:03:17,310 the latitude and longitude that I just entered in here our fourth Apple headquarters once again in Cupertino. 38 00:03:17,340 --> 00:03:21,790 So essentially we're going to start off our fake location tracking at Apple headquarters and then over 39 00:03:21,790 --> 00:03:26,820 time we're going to add in just like 10 meters 10 meters 10 meters just kind of move the user around 40 00:03:26,820 --> 00:03:29,750 the location or around the world. 41 00:03:29,770 --> 00:03:30,130 All right. 42 00:03:30,130 --> 00:03:33,440 Finally down at the very bottom this is where the real magic happens. 43 00:03:33,490 --> 00:03:37,960 We're going to define a counter variable and set it to zero. 44 00:03:37,960 --> 00:03:43,960 And we're going to set up a set interval function and I'm going to run this interval function once every 45 00:03:43,960 --> 00:03:44,440 second. 46 00:03:44,500 --> 00:03:47,280 So I'll put in 1000 milliseconds right there. 47 00:03:47,290 --> 00:03:51,870 So now inside of here this is where we're going to really trick out Expo location. 48 00:03:52,000 --> 00:03:57,540 We're going to say location dots event emitter dot emit. 49 00:03:57,680 --> 00:04:01,830 We're going to do Expo dot location changed. 50 00:04:01,860 --> 00:04:04,550 We're gonna pass in an object as a second argument. 51 00:04:04,710 --> 00:04:14,680 We're going to say watch I.D. is location dot underscore get current watch I.D. and then location is 52 00:04:15,610 --> 00:04:23,110 our get location function and we're going to pass in that counter and then after we emit that event 53 00:04:24,700 --> 00:04:27,910 I'm going to do a counter plus plus like so. 54 00:04:27,980 --> 00:04:28,220 All right. 55 00:04:28,220 --> 00:04:31,820 I know that's a lot of typing but now we can really see exactly what's going on here. 56 00:04:31,850 --> 00:04:37,280 So whenever we import this file into our project once every second we're going to emit an event directly 57 00:04:37,280 --> 00:04:42,380 into the location library and we're essentially faking out as though the user's location has changed 58 00:04:42,440 --> 00:04:47,560 in the real world the watch idea right here is just little bit of background code. 59 00:04:47,590 --> 00:04:53,050 What's more important is location location right there that's going to have our fake location that is 60 00:04:53,050 --> 00:04:56,320 going to be changing once every second over time. 61 00:04:56,320 --> 00:05:01,780 So once every second we're going to get a changed land longitude and latitude by 10 meters. 62 00:05:01,780 --> 00:05:06,460 So this is going to allow us to very easily test out all of our tracking code and actually make sure 63 00:05:06,460 --> 00:05:10,990 that hey our application works on the user's location is changing over time. 64 00:05:11,280 --> 00:05:13,930 And once again this is only for testing purposes. 65 00:05:13,930 --> 00:05:18,970 So if we ever deploy our application we would want to make sure that we do not import this file into 66 00:05:18,970 --> 00:05:21,060 our project. 67 00:05:21,120 --> 00:05:25,290 So now that we've got this Together blessing I'm going to do is go back over to my track and create 68 00:05:25,290 --> 00:05:34,410 screen and at the very top I'm going to add in an import for up one directory underscore mock location 69 00:05:34,530 --> 00:05:36,820 like so. 70 00:05:36,900 --> 00:05:42,680 Now in theory once every second we are triggering a new change in location inside of our app. 71 00:05:42,690 --> 00:05:46,920 Now I do want you to know that as we start to test this you are going to initially start to see the 72 00:05:46,920 --> 00:05:50,220 user's location on this map right here like jump around. 73 00:05:50,370 --> 00:05:54,450 And the reason for that is that it's going to eventually conflict with the actual reading of where your 74 00:05:54,450 --> 00:05:55,410 user is. 75 00:05:55,410 --> 00:05:58,170 But we'll see that in action in just a moment. 76 00:05:58,170 --> 00:05:58,380 OK. 77 00:05:58,380 --> 00:05:59,510 So now we've got this put together. 78 00:05:59,520 --> 00:06:00,960 We've got some test code right here. 79 00:06:00,960 --> 00:06:05,550 We can now start to actually write out our tracking stuff back inside of a check create screen. 80 00:06:05,640 --> 00:06:07,430 So let's take care of that in the next video.