 Hello everybody. So in this video we'll talk a little bit about how devices in general determine location or cell phone devices determine location. And then in another video we'll kind of explore a little application that both gets location permission and polls location data. So when we're talking about determining location, figuring out the real-time location, sorry for the little mess up here, polling real-time location is the hallmark of mobile apps, right? It's used for all sorts of things from navigation to having fun and gathering Pokeballs to safety and letting people know where you are. And that's one of the the great things or the big differentiators between a smartphone that you can carry in your pocket and your typical laptop or desktop computers just the ability to quickly and easily gather where in the world the device is. So in Android when we're talking about location we are talking about floating point numbers that are the latitude and longitude coordinates of the device at the current time, right? So latitude just like on your globe or your map latitude goes from positive 90 at the north pole to negative 90 at the south pole and longitude goes from negative 180 all the way to zero at the primary and then up to positive 180. So these are the readings that you can get out of these things. Now whenever you ask Android to give you the location of the device it gives you the latitude and longitude in decimal degrees. So 34 is the latitude, 0.225590 there is a fraction of a degree and usually it will also give you an accuracy reading. Okay, so you get the latitude, the longitude and then an accuracy reading which is usually reported in meters and that's by default how Android does it. So when you're talking six decimal places here you're talking down to within a meter or so. So this is highly precise whether it's accurate or not is it's another matter and how accurate it is and how precise it is depends on well which technology was used for locating the device and there's three options one is the cell tower fix the second is Wi-Fi positioning and a third is GPS and I just want to talk very briefly about how each of these things works right. So first of all the least accurate thing the cellular tower fix. Every phone knows which cell base station it is attached to one of these are really things that you see all over the planet. The phone knows where it's attached to and then there are databases out there up in the cloud that people provide as services that know where each cell tower is and of course the cell tower providers you know like 18 teen sprint Verizon they know where their towers are. So as your phone is traveling around it's pinging off of these towers as the cellular signal or the Wi-Fi signal connects the radio signal connects to each tower. So the accuracy though so we know that the phone is attached to a cell tower we know where in the world cell tower is but the range of the cell signal is quite large you know can be one to two kilometers or further so the accuracy here is quite poor. On the other hand this is basically information that you get for free. Okay so and if you've seen crime shows or you listen to the serial podcast when they're talking about pinging a tower and they know where the power tower is and then people you know driving along and figuring out where they were approximately this is what they're talking about. Okay so the second option is through Wi-Fi positioning and the way that Wi-Fi positioning works is that each wireless access point right and I got a picture of one which you can't see it because my head's over top of it there it is each wireless access point that you might see has a unique MAC address media access control address and Android is constantly scanning for nearby Wi-Fi right so is iOS right oops excuse me right and even when this will maybe surprise you even when your Wi-Fi is turned off on your phone you know you you click the little button up the top turns it off your Wi-Fi is still scanning looking for these access points and the reason is it's trying to figure out where you are because there are databases out there of known coordinates of different MAC addresses okay and so if you're attached to this guy and to this guy and to this guy and to this guy it can measure the signal strength here these decibel ratings and using what we call trial iterations just a algorithm it can figure out you know if the signal strength is this and you're talking to these guys well if you're talking to at least three of them it can kind of approximate where you are and it actually is pretty accurate it's accurate down to about 10 meters 30 meters which is good enough for a lot of applications if you think about 10 meters 30 yards more or less so you know that's pretty close the interesting question is well where did this database of MAC addresses and coordinates come from now it turns out Google got in a lot of trouble for this when they were driving their street cars around making Google Maps right in the Google Street view turns out that in those Google Street cars or Google Maps cars there were Wi-Fi connectors that were just pinging all the Wi-Fi signals all around them and as the car is driving along and say oh I'm hitting you know edgy Rome with whatever MAC address and it would say okay I hit it and they would record it and they kind of gotten some trouble for that so anyway there are these huge databases the third of course is GPS the global positioning system you may not know this but the GPS system that we all know and love is sponsored by the US government the Air Force runs it there is 30 some satellites up there and there's a special circuit inside your phone usually this circuit is actually a part of the cellular radio they come in in a little package and the circuit is just sitting there constantly listening for the signals from these GPS satellites to come down and then the circuit has to hear from multiple satellites it needs to hear from at least three but it most circuits can go up to say hearing from 12 satellites at once and again it performs trilateration a kind of algorithm to isolate alright if based on the signal strength and the distance figure out exactly precisely where you are and GPS can be really precise consumer GPS like the kinds you would find in a smartphone it's usually accurate to around three meters and that's good right you know three meters about nine ten feet professional GPS commercial GPS for surveying and then military GPS they can be precise down to the centimeter or some downside to GPS is that it is power hungry so if you're only getting GPS signal positioning that circuitry takes some juice to run and so you'll drain your battery pretty quickly if the only thing you're getting your location from is GPS and you're constantly polling for that information okay just some quick shorts that show you the accuracy of the main methods there as you can see the GPS in general is way more accurate most of its readings are down below 20 meters this is a little experiment I wrote I did in my former life Wi-Fi is a little more consistent but it never drops below 20 meters in terms of accuracy so alright so now you're writing an app and you want to ask the phone hey give me your location where am I right now Android provides this thing called the location manager which is an API which can help access provide an interface over these underlying technologies of Wi-Fi positioning GPS positioning and cell tower positioning asking for a location is typically we call it polling for a location but using the location manager location manager is not smart it just tries and gets the data it can be tricky to use in figuring out a polling strategy is tough how often do you need to update it how accurate do you need to be what are you gonna do if airplane as mode is on what are you gonna do if the user location services are turned off how do I not drain the person's battery what if the person doesn't didn't give me access to their location they didn't grant permission it's actually quite challenging gets pretty sophisticated pretty quickly and that's not even worrying about the algorithms which you don't have to worry about for figuring out where you are that's just like the life cycle of asking for a location suppose here's just an example of how complex it can be right so this is an example chain of events for polling for a location one time like I just want to know where I am right now right so the application starts it's listening for GPS and network updates network being the cell tower and oh it has an old network position or it has an old position from the network okay great I don't actually have to ask for it it's already there it's cashed but okay so I'll use that but we know that the network location the cell location is inaccurate so that's not great oh there's also a cash GPS location but it's too old it's out of date so we're gonna get rid of it we get a new cell ID fix is received okay so we got a new network location still not great but now Wi-Fi has come online maybe I've come into range of some wireless access points so now I can get my Wi-Fi location but then we turn out that that Wi-Fi based location the signal strength wasn't good so we have to get rid of it you know you shouldn't trust it oh and finally the GPS comes online we get three satellite signals it's gonna give us our current best location right so alright I've got my best location now so I can stop listening and let's just give that best estimate of location to the user right this is just an example of how complicated it is to ask for one position things you have to worry about so rather than you the developer working about it Google has provided some APIs to us one is the fused location provider and Google Play services so rather than developing your own strategy for dealing with all these possible things that can go wrong things that you need to worry about when pulling the location you can use an API from Google it's still a little tricky because you have to handle all the exceptional you have to react to the exceptional conditions like if you go out of cell phone range you can display that you'll get a reaction to it you have to decide how to handle it but nonetheless it does kind of simplify the whole process you just have to react to the process but you don't have to think about all the things that could go wrong he's kind of tell Google here's what I want to do I want to get updates every 60 seconds and they need to be at least this accurate and it'll kind of provide those things to you all right so this is an overview of some of these technologies for location services next video we'll go into just a quick demonstration project kind of shows you how some of this stuff works until then