1 00:00:00,000 --> 00:00:04,000 [Jim Hoskins] So now we've updated our code so the calculation of distances 2 00:00:04,000 --> 00:00:07,000 probably works; however, we don't know for sure 3 00:00:07,000 --> 00:00:10,000 because all of our Notes have the same latitude and longitude 4 00:00:10,000 --> 00:00:13,000 which is the same as our current location. 5 00:00:13,000 --> 00:00:17,000 All of our Notes showing here with 0 would appear to have 0 distance 6 00:00:17,000 --> 00:00:20,000 between them and the note, and this Note up here with -1 7 00:00:20,000 --> 00:00:24,000 would indicate that we don't actually have a distance on that Note 8 00:00:24,000 --> 00:00:27,000 and -1 is sort of our escape value. 9 00:00:27,000 --> 00:00:30,000 So let's write some code to change the latitude and longitude 10 00:00:30,000 --> 00:00:33,000 if we're in a debug mode. 11 00:00:33,000 --> 00:00:37,000 So what I'm going to do is I'm going to add some code at the top of our NotesApp here. 12 00:00:37,000 --> 00:00:41,000 Now, this is all contained in a self-executing function 13 00:00:41,000 --> 00:00:44,000 so we can easily create a function and a variable without clouding up 14 00:00:44,000 --> 00:00:47,000 the global scope of our browser. 15 00:00:47,000 --> 00:00:50,000 So one thing I'm going to do is I'm going to create a variable 16 00:00:50,000 --> 00:00:57,000 called debug, and we'll set it to false; at least for now. 17 00:00:57,000 --> 00:01:02,000 So now we need to actually create a function that will change our coordinates 18 00:01:02,000 --> 00:01:06,000 ever so slightly every time we call this function. 19 00:01:06,000 --> 00:01:11,000 So let's call this function fuzzCoords 20 00:01:11,000 --> 00:01:17,000 and we'll take a (coordinates){ object. 21 00:01:17,000 --> 00:01:24,000 All we need to do is call coords.latitude 22 00:01:24,000 --> 00:01:28,000 and we'll do += to add to it and we'll get a random value 23 00:01:28,000 --> 00:01:31,000 by doing (Math.random() 24 00:01:31,000 --> 00:01:34,000 and (Math.random) returns a value between 0 and 1. 25 00:01:34,000 --> 00:01:39,000 And I want them to be equally weighted between adding and subtracting, 26 00:01:39,000 --> 00:01:42,000 so I'll subtract 0.5 27 00:01:42,000 --> 00:01:48,000 and I want to scale that degree down by one-tenth so we don't move too far 28 00:01:48,000 --> 00:01:53,000 in latitude and longitude from our location, so I'll multiply that by 0.1. 29 00:01:53,000 --> 00:01:56,000 You can change this factor right here 30 00:01:56,000 --> 00:02:01,000 if you want to make your coordinates really, really spread out. 31 00:02:01,000 --> 00:02:06,000 The larger this number, the larger the actual spread can be 32 00:02:06,000 --> 00:02:10,000 and the smaller the number, the closer to the true distance 33 00:02:10,000 --> 00:02:13,000 it will be after your fuzz these coordinates. 34 00:02:13,000 --> 00:02:16,000 We'll do the same for longitude. 35 00:02:16,000 --> 00:02:28,000 coords.longitude += (Math.random() = 0.5) * 0.1; 36 00:02:28,000 --> 00:02:31,000 Cool. So whenever we call fuzzCoords and pass it an object, 37 00:02:31,000 --> 00:02:36,000 it's going to actively modify that object so that we don't have to worry about returning it, 38 00:02:36,000 --> 00:02:38,000 although we could. 39 00:02:38,000 --> 00:02:41,000 So there's a couple of places that we want to potentially 40 00:02:41,000 --> 00:02:46,000 fuzz the coordinates when we get them, and that's everywhere we do a query 41 00:02:46,000 --> 00:02:49,000 for Geolocation data. 42 00:02:49,000 --> 00:02:54,000 So now, let's add the fuzzCoords to the code where we assign the coordinates 43 00:02:54,000 --> 00:02:55,000 to each of our Notes, 44 00:02:55,000 --> 00:02:59,000 and that's done in the new form view here in our Geolocation call 45 00:02:59,000 --> 00:03:05,000 where we get our attributes, latitude, and longitude from the coordinates object here. 46 00:03:05,000 --> 00:03:10,000 So what I'll do is add if(debug)[ 47 00:03:10,000 --> 00:03:17,000 we will fuzzCoords(attrs); so this will change the latitude and longitude value 48 00:03:17,000 --> 00:03:21,000 by a small, random value. 49 00:03:21,000 --> 00:03:24,000 So to actually get this to work, we need to actually turn on debug 50 00:03:24,000 --> 00:03:28,000 and change this to true, 51 00:03:28,000 --> 00:03:31,000 and let's try it out in the browser. 52 00:03:31,000 --> 00:03:35,000 Let's refresh, go create a New Note, 53 00:03:35,000 --> 00:03:42,000 create a Fuzzed Note with some text 54 00:03:42,000 --> 00:03:45,000 and we will tag it with our location and save. 55 00:03:45,000 --> 00:03:51,000 And if we go to Nearest Notes and look at our Fuzzed Notes here, 56 00:03:51,000 --> 00:03:55,000 I can assure you this is not our current location. 57 00:03:55,000 --> 00:03:59,000 It is in a suburb slightly outside of downtown Orlando, 58 00:03:59,000 --> 00:04:01,000 so it will work perfectly. 59 00:04:01,000 --> 00:04:04,000 And let's go back to our Nearest Notes here 60 00:04:04,000 --> 00:04:08,000 and see if we click Locate if that Fuzzed Note shows a different value. 61 00:04:08,000 --> 00:04:14,000 Cool. So it's showing about 4.8 kilometers outside of our current location. 62 00:04:14,000 --> 00:04:18,000 If we were to go back and change all of these Notes' values, 63 00:04:18,000 --> 00:04:21,000 we should get a slightly different value for each one 64 00:04:21,000 --> 00:04:25,000 and now it looks like our location will work, so it's going by distance. 65 00:04:25,000 --> 00:04:31,000 I can create another Note and we'll do our Next Fuzzed Note 66 00:04:31,000 --> 00:04:36,000 and we will give it some text. 67 00:04:36,000 --> 00:04:41,000 We'll make sure it's Yes, save it out. 68 00:04:41,000 --> 00:04:45,000 If we go to Nearest Notes again, Locate to update our position 69 00:04:45,000 --> 00:04:49,000 and our sort, this Note is a little bit further away. 70 00:04:49,000 --> 00:04:53,000 If we take a look at it, it is quite a bit south of Orlando-- 71 00:04:53,000 --> 00:04:56,000 a little bit even further than the previous note, 72 00:04:56,000 --> 00:05:00,000 and I would venture to guess that that is probably roughly 6.2 kilometers 73 00:05:00,000 --> 00:05:04,000 outside of where our current location is, based on my knowledge of Orlando. 74 00:05:04,000 --> 00:05:07,000 So it looks like our Location works. 75 00:05:07,000 --> 00:05:10,000 If we were to take this out and test it around town, 76 00:05:10,000 --> 00:05:14,000 we'd obviously want to turn off the debug since we would actually be moving throughout space 77 00:05:14,000 --> 00:05:17,000 and we'd want our latitude and longitude to be accurate, 78 00:05:17,000 --> 00:05:21,000 but for testing, this debug system works pretty darn well.