Android Studio Tutorial - Build a GPS App

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome to a series here on android studio we're going to build an application that will demonstrate the gps features of your phone so you can see the app in this condition here the latitude and longitude is registered the altitude and the accuracy and the speed and also the address at which you're located there'll be a few switches here so we can turn the service off turn it back on switch it from the less precise to the most precise gps settings so that's just ahead [Music] so in this application here are some of the things that we're going to learn we're going to be working with something called a fused location provider client the fused idea is that there are multiple sources that you can get a location service so not just gps you can use your wi-fi you can use the towers and if other applications on your phone are getting location services your app can piggyback off of their work in this tutorial we'll show how to get permissions so that your users do not have to provide the gps location if they don't want to we're going to work with a class called location request and so this will set the preferences for how accurate and how often you want your gps service to be tracked we're going to install the google play services and we'll talk about what's required in making this phone application work this will not work in places like china but it will work in the rest of the world where google play services are allowed the application will rely on an event listener called on success listener so success means successfully located myself and whenever that happens we will update the ui or the user interface on the application we'll set up a method that will start and stop the location tracking service and then finally we'll work with geocode which is the ability to translate a latitude and longitude coordinate into a postal mailing address a street address so here's a preview of what it's going to look like when we're done a fused location provider will give us these coordinates and the rest of the data and your ability to control them and how precise they are so the first step is to create a layout so i don't want to spend a lot of time working on this layout so take a look at what it is here all the controls and their names i will provide you with an xml file that you can copy and paste or you can build it on your own but the point of this activity is not so much about the user interface as it is the more complex tools that are behind it so let's get started with making a new project i have android studio looks like version 361 as the current for right now so let's get started with a empty activity so i'm going to give my application a name something like gps tracking demo so for my location i'm going to put in the domain name that i work at so i'm at edu.gcu which is grand canyon university and then the name of my app so i'll let the build process settle down and then i'm ready to go so let's start with the xml activity and i'm going to the text view for this so where did they put the text view it seems like they changed it on this version so if i go to right click and choose go to xml i get the xml feed now i'm copying and pasting in all of the code that i'm going to be including in this new version of the app so let's see what happens if i click on the view to go back to the graphical user interface view i get this so you'll notice that for each of these items here i have set a specific naming convention so sw stands for switch tv stands for text view and let's see is there anything else it looks like tvs and sws are the only thing i have in this version but you can see the constraints are all properly set so that the layout is nice so you can use my xml file if you like i'll put it in the comments below so it would be nice to see all of the ui elements in the code side by side so instead of switching back and forth between these two tabs i'm just going to split them so i'm going to right click up here and choose split let's try vertically and so you can see that now i have two two windows to work with so i've got xml on this side and i can switch to java on this side so this way i can get all of these references quite quickly so first of all i'll put all the text views into place so i'm going through each item that is not a label so tv lat tv lawn altitude accuracy speed sensor updates and address are the ones i'm looking for and i ignore everything that says label now just a reminder of what all of these things are that everything that has a zero in it right now is considered a text view everything that just has the word listed over on the left side is considered a label so we can ignore the labels they're static now the two switches that i'm programming here are the switch for the location updates and also the switch called gps the next part is to go inside of the uncreate method so remember uncreate is what's called the first time that this activity is opened so we need to give all of these variables an actual id number and so those id numbers are found in the xml file so we're going to let the find view by id function work for each of these so remember to do lat lawn altitude accuracy speed sensor updates address gps and the location updates so this is kind of a tedious process but you got to go through it to make sure that all of these things work so this would be a good time to test your app to make sure that we haven't mistyped any of these values so let's run the app all right so if you get the app to run correctly that's probably a good start so we can test it out there is no activity yet of course we've just got the controls and their values set up but if you have an app crash here then double check all your work so all of that is ahead in just a moment for right now though let's pause and take a break and we'll be right back hey welcome to part two of our application that we're building called the gps tracking demo you can see the app on the right we are going to configure some of the dependencies that are required to make gps work on your phone [Music] all right before we continue on any programming there's some important facts that we should look at regarding the api that we're about to use so there are three sources of data that your phone can get its location from the most obvious one is the gps satellite location that's the one that of course works from space cell tower locations are also accurate if you have a triangulation system they can use trigonometry to figure out how far away you are from each tower and concentric circles are the idea that you can measure distances from three points and triangulate with pretty good accuracy where you might be also wi-fi locations can be used since they are known gps locations and you can find locations if you're connected to a wi-fi with a fairly good accuracy all three of these are fused together into a class that we're about to use called the fused location provider and so we're going to build that in just a minute so here's some code that we're about ready to type we're going to create a fused location provider class in our app and it has two different uh functions that are going to be chained together so you can see this one called get last location and add on success listener so if a location is found then the success listener will create another trigger to another anonymous function it's called and so these nested functions will provide most of the work that our application is going to do so fortunately a lot of the class has its work done for us we don't have to determine which of the three services that we want to connect to we can give it a general idea to say be super accurate or be super conservative with the power and this class will do the work for deciding which service is in action at the moment so we're going to be configuring a class called location request in just a minute and we are going to be specifically telling it if we want the priority to be high accuracy and how often we want to get the location updates so here is what the code is going to look like we're going to initiate a location request we're going to set its interval we'll set its fastest intervals so that way it's if there's no concern about power we'll update every five seconds instead of every 30 and then we'll set it to high accuracy so there's different ways that you can configure this and you've got them all listed right here in front of you also in just a moment we're going to have to install the google play services so this will be a dependency in our gradle dependency list so what is google play services it's far more than just the google play store or games as a matter of fact it's google's attempt to regain control of an open source operating system so even though technically anyone in the world can use android freely only the official google play services that are installed give you the functions that most phones really require for example you want your app to work with google sign in to work with locations to work with maps to play games to work with the instant messaging systems there's a lot of services that are built into google play such as showing ads and doing some security work if you don't have google play services on here you really don't have a full functioning android operating system and so it's not available in china due to trade restrictions and the whole business of relationships with governments and so if you are building an app that is for the chinese market then you probably don't want to rely on google play services really the only company that this affects is huawei and so if you want to work with huawei they have an alternative google play services and you can see it here at the link below so now i need to go find the dependencies that are going to make this gps service work so i just googled this it says android location services and gradle which is the dependency manager and we'll pick the first link so about halfway through the list i find google location and activity recognition so i want to copy this link here it looks like version 17 is the current and we're going to put this into our dependency manager so i copy this link and switch back into android studio so the place that i need to put these dependencies is in this section called gradle scripts let's open the first one and see what it says so this is some dependencies and the note says don't place anything here any individual application dependencies belong in the other file so let's go to the second file and here they are so the dependencies that we're allowed to add are down here at the bottom of the list so i'm going to press enter and paste in the location services 17.0 i can't just paste in some string here i have to put in the word implementation beforehand so i'm going to paste in implementation and then put in the little quotation marks before and after this and then at the top of the screen it says you must sync so i will choose sync now and let's see what happens i got a configuration successful so that's great hopefully yours worked as well if not then you might have to rebuild or clean your project or restart android studio sometimes that occurs but we've got a success on mine so we're going to keep going now let's switch back into main activity we're going to be able to use a new class it's going to be called fused location provider client and you can see that it's providing me some type ahead here so i'm going to choose this and it automatically imported something up here on line number nine it says it came from the google gms location service so i'm just going to put in the word fused location provider client as the name so i'll just put in a node here so it stresses the importance that the fused location provider client is the heart and soul of this application the majority of the app's features depend on this this would be a great time to check to see if the application still works so i'm going to click the run button and see if there are any errors with this new class it looks like it launched successfully so i couldn't ask for anything more than that so let's keep going the next class that i'm going to import is called a location request so the location request is a config file really it's a class that has lots of properties that will influence the way that the fused location provider works so at this point we're just going to declare that we have a new class a new instance of this class now that we've declared it in the top level of our app we're going to do the initiation down here after we've gotten all of the values of the ui settings the first two things i'm going to set up are called the set interval and we're going to set this to 30 000 milliseconds and then the second item is called set fastest interval which means if we are using maximum power and maximum accuracy how often do we want to update an alternative to putting 30 000 in here would be to put in a thousand times 30 just makes it more obvious now just for showing a good programming practice i'm going to change these values from hard coded in this part of the function to a constant so i will refactor choose extract and constant and both of these can be set at the top of the page then so after i've extracted these two i should be able to find what i put in up here yeah so we've got ourselves the default update interval and the fast update interval is 30 and five so if i have to make any changes later this is a convenient location to find these values the next item that i'm going to choose is called set priority and in set priority i have to specify location request dot and i can see some other constants that are available to me so by default i could set it to the balanced power accuracy that's probably what i want to choose high accuracy and low power and no power are other choices that we can use here so so far we've seen several things in this video we've imported the location request service and we've done some configuration here so in the next video we're going to configure these buttons here that say location service updates and the gps and wi-fi options and so that'll be coming up in the next video hi in this video we're going to do some more work on our gps tracking app this is the third one and we are going to start configuring these buttons here that allow us to switch from cell phone tower accuracy to the gps accuracy and then maybe we'll actually see some events here happen with our location services so that's coming right up [Music] so in this part of the video we're going to start attacking this button right here that says gps and save power which is the ability to switch between the gps service and the cell phone tower tracking so let's put in a click listener for this guy so to put in a click listener we go ahead and type in the name of our switch which is sw underscore gps and then inside the parentheses of our event listener we're going to add a new click listener so now let's do the code for the inside of our switch so we're going to check to see if the switch is checked so checked means it's turned on if that's turned on then we want to be setting this to use the gps sensors or we call that priority high accuracy in the vernacular of this function here so we're going to use the constant called location request dot priority high accuracy then we're going to update the text view called sensor and sensor tells us which service that we're currently using and so we're using gps sensors and of course then else is going to do the opposite to say hey we're not going to use the high accuracy we're going to do a balance between power accuracy and the the priority of uh getting more accurate results and then we're going to tell it that the sensor is using the towers or wi-fi this would be a good time to check to see if it actually works so let's turn it on and run it so we've got ourselves power and gps down at the bottom and you can see that the the label is set correctly and we hope that inside the internals that the variable is also being set correctly but we won't know that until we get a few more lines of code all right so now we're going to create another method and i just want to mark the end of our uncreate method here because it's going to get longer and longer here so i'm just going to put in a comment so the new method is called update gps and it does several things first of all its goal obviously is to update the gps location so we'll have to ask for permission from the user before we can make this work and then we'll get the current location from the uh the fused client and then we'll finally update the ui or the text views so that'll display all the values on the screen so you can see we're getting closer to actually making this app work so the first thing that we have to do is invoke a variable that was declared at the very top of the application so it's fused location provider client remember this is the heart and soul of how this application works so now we're going to actually assign a value to it it comes from something called location services dot and we're going to get the location provider client and that is associated with a context and so we can either put in the word this or mainactivity.this so the first thing we start with is the permissions issue so i'm going to put an if statement to say if the activity compact that's the that's the class that is our application if that is going to get a permission check to say were the grant permissions granted for access fine location that's the if question do we have permissions if we do then we'll continue on to get those locations if we don't have permissions then we need an else statement and we're going to have to go request those so that's some things yet to do so let's assume that we have permissions from the user so we're going to now call our fuse location provider client and use the method called get last location so whether that's five seconds ago or 30 seconds or 10 hours we're going to take whatever it has in its memory and then we're going to use an unsuccess listener so we're chaining these things together with dots and then inside of there we're going to have another unsuccess listener item so this will eventually feed us to a variable called location so the location uh variable that you see in this parameter will contain lots of things like latitude and longitude and altitude and speed and the other things and so we'll be able to take that location class and update our text views so we'll do that in just a minute that's still a to do so now let's do the work for getting the permissions so first of all there's a catch here you have to check to see if the value of your current android operating system is sufficient so we need to be at build number 23 or higher and so the build code for 23 as you can see is the letter m now if we have the correct version of an operating system then we can go ahead and get the permissions so we're going to use the request permissions command and then request permissions comes in the form of an array and the first item of the array is the location or the the the type of permission which is access fine locations and then we need to provide it some other number so we could use a number one or two or three whatever we're going to make a constant we're going to call it permission find locations now you can see that we have several things that are underlined in red so these are errors that we must resolve so for the first one to resolve i forgot to put in sdk underscore int so build version integer has got to be greater than 23 okay my mistake then the other record goes away and then finally this permissions fine is not set so we need to make this into a constant so we have a choice that says make this a constant when i make it a constant it shows up at the top here and we have to provide it a value so let's create something i'm going to call it 99. you can use any arbitrary number you want well now we're going to have to have another method here that will actually go and request these permissions so let's go up to the top of the screen and we're going to override a method that comes with our activity so finding line number 20 which is our activity so app compat activity is the base class that where our main activity is based on so i'm going to right click on it and i'm going to choose generate and this time i'm going to choose override methods now there's lots of methods that can go into a main activity as you can see here so many to choose from what do we want so what i'm looking for is the on request permission result and what that does is it tells the program to trigger a method after permissions have been granted so let's see what comes in the form of this new method that just showed up here on line 92. so the key part to recognize in the permission request with result is that there's a request code which is going to be the number 99 that i had assigned earlier and then the permissions which is which thing did we actually get permission for and then the results all right so let's get on with the code inside of this function so first of all the request code is important so there could be many different requests that we've gotten and we're only interested in number 99 so in our switch statement we're going to say check the case for permissions fine location or we could type 99 either one will work now if the request was from that permission then we are going to update the gps so that's the function that we just got done coding a minute ago and then if if they didn't give us permission if they denied them then we'll just give them a toast message and exit the program let's scroll down to where the update gps function is so we didn't do anything here once we got permissions so this is a whole bunch of stuff to do we could code everything right here we could just update all the text views however i'm going to provide this as a separate function just to keep things a little bit more compact so the new method pops into existence down at the bottom of the list so it's rather arbitrary where it goes but that'll work and i'm going to put in a comment to see what the purpose of this function is this method so we're going to update all of the text view objects with a new location so where's the location at so what i need is a parameter so i'm expecting to get a location from the other functions so let's put in the parameter here and then go back and patch up the other reference so it says you want to do uh update values so you're missing something you're missing a location well do we have a location yes that's what this variable above us gives us location is given here so location location location okay so i'm going to save that and then let's see i don't see any more errors so this is some to do work that we'll have to come back to in just a moment but don't forget that just because we asked for permissions here uh they're not yet defined we have to go into our manifest to fix those so let's go and look in the manifest so i'm just going to type in uses and then magically it seems to know what i want to do uses permission and then the name of the permission we're looking for is access find location and then we close the tag off so we can close it as you've seen it written here or i could put in just a slash close and that's a self-closing tag so either one of those will work okay so that's the permissions i probably don't need to see the manifest anymore so i'll close it now let's just finish off what we started here we're supposed to update the ui values so let's go add those now so what i'd like to do is set the text for tv lat so remember text view latitude is what i'm talking about and i want to get it from the location dot get latitude you would think that would work well things aren't always that easy so the problem is get latitude is going to return to us an integer and set text doesn't automatically convert from integer to string so i've got to go in here and parse it so the way to parse an integer into a string is to use the string method called value of and we'll surround the integer that we're trying to get so i'm going to do two others that are fairly similar so i'll just copy and paste so the second one is get longitude and so we'll just change a few text items here and we got longitude the third one is accuracy also is just an integer that we're going to display as a value of how accurate the computer knows its accuracy is now the next item that i'm going to accomplish is the altitude and it seems like not every phone has the ability to get the altitude so we have to check to see if it has an altitude they actually made a function called has altitude if it's set to true then we'll get it if it's not set to true it means or it's probably null then we will just say it's not available the same kind of thing holds true for speed if your phone can calculate the speed then we'll display it if it is not set it is not a value it'll be null and so we'll just say not available so we've got ourselves this final product almost we've got ourselves the update val ui values and that was called from this method called update gps so we could have put all of that code in inside here and update values but we might want to use that again somewhere else so that's why i separated it into a separate function but if you want to put it all together it'll still work now we've got to use this function so i've created the update gps but have we actually called it anywhere i don't think so so a good place to call that would be right at the end of our oncreate method because everything's been set now we're ready to do update gps so i'm going to save this and let's see if it'll work i'm going to run it alright so you should get permissions to show up i've already run mine once and so they're not there but i did give it permissions and you can see i have a gps location here so this is the the default location that is found in the settings of my virtual phone if you put this on a real phone you'll get the location where you're at and so it appears that mine's working so location updates on off is not doing anything yet we still got that to go the second switch is theoretically working however i really can't test it on this phone because it's a virtual phone but on your real phone you can check this out so we've got ourselves a one-time update it is just checking when the app launches that's pretty good but we want to have the app to be able to turn this service on and off which is this switch and we also want to be able to track this as it's turned on so every 30 seconds or every five seconds and so that's still some work to go but we'll fix that in the next video so stick around and we'll refine our application to be even more useful than it is now hi welcome to part four of our gps tracking demo you can see the app on the screen it's got a gps location in this video we're going to be able to turn this service on turn it off and to be able to configure how often it runs and so we've still got some refining to do but we'll finish this up in just a moment all right so we have to go and program this button here that is the turn on and turn off so that's a click listener let's go into our code and see if we can put that in our our on create method so we'll create a click listener and then inside there we'll add a new anonymous function or an inline function called onclicklistener so when the unclick item occurs we will ask the question is this check switch checked if it's checked then we want it turned on if it's not checked if it's turned off then of course turn off tracking so i'm going to once again create helper functions so in this function i'm going to say let's let's make a new one called start location updates which will turn on a bunch of things and then we'll also have the equivalent turn off thing so we'll stop location updates so we've got to create these functions separately so let's go and see if we can get some code helping i'll click on the first item and we'll choose generate a new method and we'll put it in the main activity and we'll do the same thing for the stop location updates so we should see two new methods in our program so let's make this obvious let's put in a statement to say update at one of the text views it's called uh tv updates and its status is to say hey we're being tracked or we're not being tracked so we'll put that in that doesn't actually change anything it just tells the user what's going on now the second statement in this method should be called fuse location update client dot and then we're going to request location updates and this method requires three parameters first of all it says give me your location request remember that's all of the parameters such as how often this is occurring and how accurate it should be so we defined that a long time ago the second item is called a location callback and we have not created this yet this is going to be yet another method that will trigger every time this location changes and then the third thing is called a looper variable and uh honestly we're just going to put the word null in and i got that from the the instructions in the guide of how this is supposed to happen so this might be a good time to show you where some of this information that i'm putting on the screen is coming from so i'm getting this from the google documentation in their tutorials so you can see that i'm at the page that says we're looking at get the last known location let's see if we've done any of this already so it says you must specify your app permissions and sure enough we did that right here we accessed they told us to use course location i used fine but mine's a little more accurate than theirs then we've got ourselves a on create we created a location service so we did that a while ago we had this same function called get the last known location so so far so good of course that's just part of their instructions let's click on the next and let's see what comes up so this says how to configure your location settings let's see what they've got they said oh don't forget to ask for permissions and then down here it says set up a location request and if you look at the code here this should look familiar we created things that look like this here we have ours is 30 000 which is 30 seconds or 5000 for the most accurate and so we've got one of these things already configured so so far so good so here's another page that now we're going to get some information from that'll help us so we're going to get the location updates so not just when we launch the app but every period of time so it says here make sure that you got permissions that seems to be a theme in their locations okay then we have a location request so let's let's take a look here what they're asking us to make so create something called a location callback and then when you have that going we're going to have a a function that goes inside there so we're going to kind of use this as their model so let's return to our code and we need to make a location callback so that has to be declared somewhere let's go to the top of our app and let's put it in here in the list of things that are kind of global to our app so now inside of the uncreate method we're going to create a new method that is configured here so that is the location callback it is a new location callback and i'll put in some notes here that say that this event is triggered whenever the update interval is met so this is 5 seconds or 30 seconds in our case so let's see what's available that we can do inside of the location callback so i'm going to click on the name right click it choose generate and override methods so this will tell me everything that you can do inside of here the two at the top are the ones that are interesting to me so on location result is the one that we're going to implement and location availability would probably tell you whether or not this is actually working so i'm going to just choose the first one there it is so now we have on location result and we should get the location results right here in this variable so now i'm going to treat this location result as a new value that we can update our app with and so i'm going to save it in a variable called location and then i'm going to pass that on to the update ui values method so that will just redisplay all the newest values now if you look at this you're going to say hey why did you save that as a separate variable and location you could have just called it inside of the parentheses certainly could have so let's see if we can make this code a little bit shorter so i'm going to copy this and overwrite location and then we can delete this entire thing so we could make it down to one line whichever you like alright so that should work let's go look at one more case when we had the we asked to turn off the gps tracking so we should update a few things here so this part of the program here is to stop the location updates and so we're going to set the all the properties of all the text values to show location is not being tracked so that is for the latitude the longitude the speed the address the accuracy the altitude and even the sensor and finally at the very end we're going to say fuse location provider dot remove location update so this is going to say hey no longer are we interested in doing the location callback function so take that out oh my goodness i'm looking at my program and i did it exactly the opposite way that i intended to you notice here this is called stop location updates and this one's called start and the code inside is totally the opposite so am i gonna how could i be so dumb let's let's copy this out of here and put it down below and then copy this and replace the stuff that's up above and let's see then we can delete this here so i think that's a little bit better okay so stopping says no no tracking starting says yes we are being tracked and then we should update the gps oh my goodness well let's see if that works all right it looks like it's running so we have a gps location and let's check this switch that says hey nothing's working so all they all go back we'll turn it back on and we got more locations so we have one more part that's not seems to be working here it says the address and so maybe we'll leave that for the next video but that will be to check to see what the street address is based on these locations above here so we'll come up for that in a moment hi in this video we're going to update our gps tracking demo to include the address of a location so right now we don't have a street address by the end of this you will the key to make this address thing work is we're going to use a service provided by google which will automatically translate things for us it's quite simple actually so i'm going to embed a little more code inside of this section called update ui value so what we're going to include now is called the geocoder so it's a class that's provided to us in android so geocoder is a new geocoder and it needs some kind of a context so let's put in mainactivity.this now this isn't guaranteed to work so we're going to have to insert a try and catch so if there's an error a program won't crash at least so the geocoder is going to provide us theoretically with a list of most recently seen addresses and so we're going to define a list of type address so we're going to do geocoder dot get from location and we have to provide it a location so we know a location it's in the location variable and we'll do get latitude and get longitude and you can see that the parameter is asking for the maximum results and we only care about one the most recent one so we'll get one now i've never actually been able to get more than one address so i'm not quite sure how the geolocator actually provides a list of things but anyway i only care about the one that was most recently seen so then the next job is to provide a text update so we have a text view called address and we'll set the text so we'll get the address number zero so the first one in the lists so that's item zero and then as you can see that there are lots of pieces to that address so we could get the county this city we could get this zip code we could get all those things for right now i'm just going to get the street address but go ahead and experiment with the others if you want to add more than just the address line if for some reason this whole thing fails i'll just say set text and will point to the user to say hey didn't work now we could do the capture of the exception and send it off to another level but let's leave this as a simple app let's see what happens when i run it now so you can see that i have the same gps coordinates the latitude and longitude but the location is actually found in seattle well you can guess i'm probably not in seattle right now i teach at grand canyon university here in phoenix arizona so if you want to adjust your virtual phone to go somewhere else you can there's a settings button down here there's three dots here and it allows you to adjust so seattle as you can see is the default location if i want to search for somewhere else let's go to london and let's go to uk so we're in downtown london somewhere let's save the point and click ok so i have this other point i'm going to choose set location so that will supposedly set the location inside of my phone so do i have to wait for it to update let's see if i change the number of seconds that it's being updated at let's see if that does anything let's turn it off and start again and there it is okay so now i got a new location so try it on your real phone you'll probably get better results than a virtual phone but you can move around the world at light speed if you want to using your virtual phone okay so that gives us a pretty good starting point for building any kind of a gps application so this will save a location now think about how you would build a real app you might have a list of recent locations we'll do another tutorial that will actually put up a map on the screen and you can show the locations and save them as you go so you could like leave breadcrumbs so one more to come but for right now this will get you to the basics of how to get this fused location service working and to get permissions so i hope you had fun this will be another tutorial in the coming minutes here about working with more gps apps in this tutorial we're going to extend an existing application and we're going to show maps [Music] so in a previous version of this gps application we had an ability to capture all of these attributes of our location we were able to turn off and turn on the location updates set the settings for precision so it could use gps or the wi-fi towers in this version of the application we are going to have the ability to show a list of all of the places that we've visited and then we're going to be able to show a map that will point a pin point where each of those locations is and then finally we have a button at the bottom that says drop breadcrumb which means wherever the current location is we're going to add a new pin to the map and a new item to the list and so that's what we're going to do in the next few videos now if you haven't already completed this application as the gps demo then you need to back up and there were like five different videos that will show you how to create this so we're on video number six right now so i'm transitioning back to the version of the application that was at the end of last tutorial so at this version we just had latitude longitude speed accuracy and the address we were able to turn on and turn off all of the different options and so now we're going to add those abilities to have the list of things that we've breadcrumbed and also have a list on our map with pinpoints so that's what we're going to start with now so step one here is i'm going to show you how to add a global address list so that way every activity in the in the program can see the list so what we need to do is extend the application class and make a place where we can put global variables so let's add that class now so i'm going to switch over to the folders where we have all of our java applications and let's see what i'm looking for is where we have our main activity yeah so this is the place this is the folder that i'm looking for so i'm going to right click and add a new java class and we're going to call this thing my application so i'm going to extend the super class as application which is going to allow us to choose this global list so you'll see how this works in a minute okay so you can see that i'm using github so i'm getting this error that says hey you just added something do you want to add it to your repository you might not see that all right so here is our new class we have main activity which is what we've been working on and now we have my application so my application extends the application class the first thing we have to do is set this up as a singleton a singleton means that there's only one instance allowed and we can create the instance the first time we call this object but after that we're going to make sure that there is only one so a singleton is a is a standard kind of uh programming method so if you google it you'll find other examples in other languages but we need to have a proper or method called get instance and then we're going to uh assign it this this property called singleton which is uh instance of itself my application then if we go to the uncreate method so when this thing is first made we are going to refer to the singleton as this which is this class so this here is the properties for a singleton now after we have the singleton created we can add some variables that will be treated as singleton variables again they will be kind of like globals now the global list that we're interested in maintaining here is a list of locations so you remember from the previous view of this app we're going to have a list of locations or location breadcrumbs and so the list type that we're looking for is of type location so we'll just name it my locations then we're going to generate getters and setters so that way we can get the list and we can set the list or we can add items to it but the point is that this list will be a single source of where we've been where we dropped our breadcrumbs so one activity can see this as well as another now we have to make sure that this list is initialized so the uncreate method seems to be the right place for this we will create a new arraylist and it can be empty so at least it's not null so let's return to the main activity and we're going to add a new button called drop breadcrumbs so let's come into here where the all the controls are and let's put in a couple of text items and then a button so first of all i'm going to put in a label and then a text view that will count the number of breadcrumbs that we've dropped so we could call these breadcrumbs or waypoints they're kind of used interchangeably here so the first one is a label so i'm just going to call this text as waypoints and we're going to set the constraints then it'll fit here below the sensor and it'll stick to the left side of the screen then the next text view will actually be the number of waypoints that we're going to collect or count and so this guy here will have the text of zero and we'll make sure that its uh constraints are stuck to the top of the previous label and right to the edge of it so now we've got ourselves two labels or we've got two text views one's a label and one's a number zero so what we're going to do with those well now we're going to count the number of items that we've dropped in his breadcrumbs well now we need to create that button so we can do the dropping so i'm going to drag a new button in so this next new button that we're going to put is called the new waypoint button so let's set the constraints so that way it is just below the label and the number that we've created let's set the width so that it extends to the left and to the right of the screen and the constraints will make it the full width then for the button name of course let's create a button that's called the new waypoint that'll be a good id for the button and that'll also be good text let's create another button below that and we'll call that show waypoint list so once again we'll set the constraints so that it fills up the entire width of the screen and fits nicely below the previous button and then a good name for the id on this button should be like show waypoint list and that could be a good name for the text as well all right so now we've got ourselves two buttons one of them is new waypoint and that will save the current location into the list and the second one is show the waypoint list and we'll probably get to that one in the next video but now we want to start programming these buttons so the new waypoint button is our first target so let's go into the main activity and start doing the code so main activity needs to have a reference to this so let's get to the top of the screen where we have the references so i'm going to add two new button variables here the first one is for the waypoint so new waypoint is the id that i used and the second one is show waypoint list and both of these have the prefix of btn in the uncreate method for this activity we need to then assign these values to the variables so we'll use find view by id for each of these guys assign them and so that now the button variables have been configured and initialized correctly so now let's go ahead and program the button listener for this button so we will do the set on click listener and we will add a new button click listener inside the method called on click let's put some comments to see what we're up to so first of all we're going to get the gps location the current thing that we're talking about and then we're going to add that to a global list so really that's the focus of this video here is we're trying to capture a list of items called breadcrumbs or waypoints now i want to create two class level level variables so let's scroll up to the top here and the first one that i'm going to create is a variable we'll call it current location and so we'll update this variable every time the gps is updated from the callback methods so this will be an easy accessible variable that all activities can get to then we will have a list of saved locations so this will be a list and it'll be a type location and the save locations is a great name for it now you're wondering what is this location thing again this is the class that is used by android maps and gps a location contains a bunch of things the latitude the longitude the altitude the speed if it's available and so it's a class that contains a lot of information about a gps point all right so where would be a good place to update this thing called current location well i think there was a callback method that we created in the previous videos and let's see if we can find it so down below way down here we have update gps and this looks like the thing so this function here or this method called update gps has the listener in it so we have the get last location going on here and let's go see if we can put this in to our current location so we're doing update ui values let's also put the current location here as this value called location with a lowercase l so this guy here is the current location so let's just save him to a variable that's further out all right so let's go find the click button that we were already programming earlier this is the new waypoint so in the comments in the unclick it says get the gps location so there should be one in that variable called current location then the second is add it to the global list so this might look a little strange but we're going to try to get access to this global class called my application so let's create a reference to it and then all we have to do to get this variable assigned a value is to call get application context and cast it to my application all right so that should provide an access to that class now if i want to do the saved locations i'm going to be able to get that from my application and we should say there's a getter in that class get my locations perfect okay so save locations now is the little global list now the last step is to take the saved locations and we're going to add an item to it which is the current location all right so we have this global list of locations and when we click the button uh we're going to add a new item to it now this will still cause an error we have to do one more step that i'll have to show you in a minute we have to change something in our manifest but the the general idea should work now i'm going to borrow these uh two lines here and reuse them in the ui so let's go down to the class that says where we're going to update the ui values right so in this location i'm trying to update the number of items in the list of waypoints so i need to add a text view and what was it called waypoints count or something it doesn't look like i've added that yet so let's let's go back and fix that problem so i've got to go to the very top here and add another reference to an item in my layout so i forgot to do that earlier so let's go to the text view items and let's call this thing waypoint counts i don't i don't know if that's the actual name for it but we can use that and then down here in the list of assignments we can do this text view waypoint counts okay i called it count of chromes that's the actual name so we'll use that count of chromes okay now let's go back down to the area where they're doing the updates on the ui so near the end of the program so what did i want to do i wanted to say the waypoint counts and set the text so the goal here is to show the number of items that are in my list so let's do the savedlocations.size and don't forget that we have to convert this into a string so i'm going to use the integer.2string method and then we should have an actual string that we can assign to the text so that should update the ui all right so i told you there was one more thing that i have to fix so i'm going to run the program and it should crash all right just as i predicted the application crashed let's go look at the logcat and see if we can understand where the error is because for sure you're going to see one of these items when it comes your way if you're using this global class all right so i got way too much stuff here i'm just gonna click on the trash can and let's see i'll re relaunch the app and maybe we can get a message that i can find here okay so we got the app running again and it crashed the second time now here is the error it says application cannot be cast to this thing called my application so there it is on line 262 this thing here doesn't work now i promised you it would and let's see if there is any helpful hints on it all right so there are no helpful hints to know what you're supposed to do here but i'll tell you what what the problem is let's go into the manifest as i promised and what we need to do is we need to give the application a name so let's go into the properties and we're going to call this thing name at android colon name and there it is it's suggesting my application there it is that's the problem isn't that a weird kind of a thing but anyway that will cause the error to disappear and let's see if we can get the app up and running now okay there we go so the location is right now at building 84 there are zero waypoints let's choose new waypoint and let's see the ui has not been updated so let's just shut this off and turn it back on and there we got one waypoint let's do another one so every time the app updates and gets a new location the ui should also update so i think that happens about every 30 seconds right now so i don't want to wait 30 seconds i'm just going to shut it off retrigger okay so we got two waypoints now in the next video we're going to show that list and then the video after that we're going to create a new button and we'll show them on a map so we're getting closer so what we got here was the ability to save a waypoint in a global list we'll see you in a minute in the next video hey welcome back to another android video for gps we're in the middle of a process of creating an application that does gps tracking and a demo so we created this waypoint button in the last video which allows us to add a new waypoint and so you can see that now we have three waypoints now in this video we're going to do show waypoint list and that will show all of the items that we've saved all right so let's get to this point here where we can make this button active and show the list so let's do a intent we'll make a listener on this button and create an intent to go to a new page so i might as well stop the app here and let's go and create that new page first of all so let's go into the folder where our java stuff is and i'm going all the way down to activity and let's choose an empty activity so empty means you know obviously we can start from scratch so i'll try to name this as well as possible so it says show saved locations list and that should give us a pretty good description of what his job is so show save locations list is going to be very simple let's go to its layout and then we're going to add only one item we're just going to add a list view so here it is we've got an empty screen and i'm going into the legacy area and choosing list view dragging that out let's just change the id here let's just call it lv underscore waypoints so we know what what's in there all right so let's just uh leave that alone and i think we should probably do the constraints so let's see if we can pin that to the top and let's pin it to the side and to the right side now i'm going to intentionally leave no constraint at the bottom so it can fall off the edge of the screen because it's going to be scrollable all right so that is the entire layout it's going to be the simplest thing possible and the only way to to return to the main screen is to click the back arrow so we don't have to create a button to go back but we do have to create a button to get here so let's go back to our main activity and let's add some items to this button here we're going to make this thing a new button listener so show waypoint list is the id number let's go program it so back into main activity and let's go find a good place to put this so i'm thinking near the top of the screen is probably a good location so let's go and add our new button listener so button show waypoint list is the name and we will set a click listener on it and add a new click listener now inside of on click we're going to have a simple program we're going to say give me an intent to go to another screen so this intent should be pretty simple as you might remember from previous lessons an intent needs a couple of things it needs to have the context so the context is coming from mainactivity.this and then it has to have the name of the class that we're trying to show and so the class name is show saved locations list we just created that a second ago and then we just have to tell it to start the activity we don't have to send any data we're using a global lists that we've saved and we're going to access that in a minute all right so there we go we got ourselves the button listener let's see if it works and if it works then we can start programming the list all right it looks like we got ourselves the application running let's save a waypoint let's do a show the list and see if this works all right so it shows an empty list and i can return to the main program so far so good now when we go to this list here let's actually populate it with the items that we've saved so as we did in a previous video we're going to have a global access to this list so i need to get a reference to my application so my application comes from this function called get application context and then we cast it into this class called myapp then once we have that we can get the saved locations using the getter so get my locations remember this is a static class or this is a singleton so that way we can get it from anywhere in the application now the next thing we're trying to do is add this list to the list view that's supposed to be on this page so let's create some variables to access the list view so let's create a variable at the top level and we'll call it lv underscore saved locations that might or might not be the exact name i used in the layout then in the oncreate area we're going to go find this item in the view or find view by id and it looks like i called it l view underscore waypoints so the names don't match exactly but it will still function correctly so now what we have to do is connect our list called saved locations to the list view and the absolute simplest way to do this is with what's called a simple list adapter and it's not fancy but it is all we need for right now if you want to create a more fancy version with graphics and everything go see another tutorial on creating a custom list adapter so then what are the parameters inside of this arrayadapter there are three of them the first one is the context so the context is just the reference to this activity so i'll just call it this and then the second item is what layout or what type of adapter are you using so i'm going to choose a pre-defined one and you can find this in android.r.layout. and you can see there are multiple different types of layouts that are predefined for us now the one i'm going to choose is the only one that i've actually have any experience with is simple lists item one and it will take whatever object you're doing and make a two string out of it so it's not fancy but it will show the data now the last item in the list is the list itself so save locations is what we're looking for so that should assign all of the items in saved locations to this list let's run the app again and let's see if it works okay looks like the app is up and running i'm going to choose new waypoint and then show the waypoint list and sure enough you can see that there is a location that is there let's go back and add another waypoint and choose show the list so we can see one there and one there so there are two now so we've got ourselves a successful list now as you could probably argue this is a very useless list what can i tell from it all i can tell is that i have a whole bunch of data that relates to a gps location i have latitude and longitude the point is that i now can see which waypoints i've saved now in the next video instead of showing a list we're going to put these on a map and that will be a whole lot more graphical and understandable but for right now we've got ourselves the goal that we met here of adding this button called show waypoint list and it's exactly what it does so we'll see in the next video where we create a map and show all the pins on the map hey welcome back to another in our series on our gps tracking demo in this video we're going to add a new button that says show map and it will show all of the waypoints that we've saved and put them as pins on a google map [Music] so let's get started right away by putting a button on this screen and then we'll create a new activity that will have the map so let's get into our main activity area and let's go ahead and add this new button so this is a pretty standard button we're going to put the anchor points in so that it falls next in line so there'll be three buttons in a row then let's set an id called button show map with the constraints of course and let's do the full width of the screen and then for the text let's put in show map now let's go into main activity and program this so we need to give a reference to it at the top of the screen and then program it so here we go with that reference so after the reference variable is defined at the top then we need to give it a value in the uncreate method we've got a whole list of things that are already being created and so let's choose a new one find view by id and it looks like btn show map is the correct value from the layout so now we've got ourselves a reference to the button now we have another button that's almost doing the exact same thing that this one is the show waypoint this one here so we're going to use this as a model so let's go ahead and add a new button listener so all the code will look pretty much like we did on the previous function so we're going to have a set on click listener a new click listener we're going to put inside an intent in there so the intent is going to say what is the context which is mainactivity.this and then we have a problem because we don't have the new activity that we're trying to get to yet so this is supposed to be called maps or something like that but we'll have to leave it blank and we'll go create the map and then we'll come back and finish off this button click listener all right so let's create the map so we're going to have to leave this little error and it says we're expecting an expression so let's go create the name of the class that we'll actually get to in a minute so let's go up to the java folder and right click and we're going to add a new activity this time and let's go to the gallery let's see what's in our gallery so you can guess that we're going to do an android maps activity let's choose next we'll just leave it as maps activity as a good enough name and finish okay so we got this map activity going and it says here we've got some to do's before we can get anywhere um anywhere close to using the maps so it says you need to have a google maps api key so you have to register your application with google in order to make it work well they make it so simple they have this link here you just copy the link and bring it into your browser so let's bring up a new browser here and paste that link and let's see what comes up so we're going to have a registration process so if you don't have a google account uh you don't have gmail well you have to get something like that so we have a create a project here as our option and let's just choose continue this will register a key name for us that we can copy and put into our project so if you have to go through the registration process of setting up a google api that's another issue but you're trying to get to this screen here all right so it says your api is enabled create the api key and let's see what comes up oh perfect okay so now i have an api key i'm going to copy that so there's a little copy icon and i'll just switch back into my application so i'm switching back and now it says this place called your key here so i'm just going to paste that now your key is going to be different than mine so obviously don't use the one that i'm using on the screen because it'll probably go away all right so we got this first step involved we got an api key created now we're ready to go ahead and do some programming well where is this new class it's called maps activity and they've already given us some code how nice tells us that we have an uncreate method and we've got ourselves a sample of this function here they put some data in for us and they've added sydney isn't that nice so sydney is located at negative 34 151 on the gps coordinates that's the latitude and longitude so there is a class called lat long okay and they put a marker on this thing called marker in sydney and then these and they move the camera let's see if this actually does anything so let's go back into main activity and now we can add the reference to our new guys so it's called maps maps activity come on maps activity dot class all right so now the intent seems to be happy let's see if we can get to our new map and see what sydney looks like on the map okay the app is up and running let's just go straight to the show map button and there it is we have a map and sure enough it looks like sydney so i'm going to try to zoom in as best i can with this emulator it's a little bit odd now we're going to instead of putting sydney on the map we're going to put the points on that we've plotted using our gps tracker so let's go do that next so let's go back into our map program so maps activity and instead of showing the marker here for sydney and for this guy let's just comment them out because we might want to see them as an example but we want to use our own so the first thing i want to do is create a variable called savedlocations let's put it at the very top as a class variable and we can assign and read from that as we need to so we got saved locations as type list then let's go into the uncreate method and let's go and assign that to the global list that we've been working with so we're going to create two lines that will get us access to that list so my application is the first variable we need to create and we get that from our get application context command then the next line down is we're going to do save locations equals and we will get the locations from the global list so now save location should have data in it now once we have saved locations we can go ahead and add them to our map so now i'd like to create a for each loop and go through the items in the saved locations so when i type four each the ide helps me out with some code typing so it looks like in java we have to do a colon and that's how the for each loop works so we have four the first item is the counter variable or the each variable so it's type location is what we're expecting and then the list is after the colon so it's saved locations now i'm going to look at the example that we used for sydney so sydney used a type called lat long and so that's we're going to create as well so we'll create a new lat long variable and it is going to need the gps locations from our location item so location.getlatitude location.getlawn will give us exactly what we want so now i'm going to create a new map marker and let's take a look back at what they gave us for the sydney example so we're going to we're eventually going to call add marker that's the name of the function but inside the parentheses you can see that there is a variable called marker options so let's create a new marker options to start with and the marker options allow us to create a position property so marker options dot position and we have the lat long available now i'd also like to add a title to it instead of saying the marker in sydney for the text i'm going to create a new text and we have the lat and long so i'm just going to use that as the title so that way when we show the variable or we show the pin in the map it'll show us the location and we we could have put some other thing in there but this works as well as anything for a title finally we do the map m-map and add the marker and so this should add a new item to the list let's go ahead and launch this and see if it works we might have to refine it but i think this will work okay the app is up and running and we're let's gonna add a new waypoint and then let's go show the map so the map shows up and we have do we have a waypoint there it is it looks to me like it is in arizona which makes sense that's where grand canyon university is now let's uh let's go back here and let's add another location so as you recall we can set our location on the gps for our phone emulator to any place we want so let's let's go ahead and put in something in london and let's see what happens if we save a point here and i'm going to call this thing london so i got london in my list and i'm going to set the location all right so it's just like my phone went at light speed and is on the other side of the world now so if i turn the gps off and on uh it still says grand canyon university that's not what i meant so let's go and use this set the location come back and let's see what happens here do we have do we have a new location at london okay let's turn this off turn it back on let's see if we can get this to save so in a real phone this actually works better so let's go and look at the list and you can see now the last item in the list is a different location so that that looks good now let's go look at the map and we should see something up there's one tagged in london and there's something over here in arizona so yes we do have two locations now and the list seems to be working so this is a lot more fun with a real phone if you go out and you follow your phone around you can track and place little breadcrumbs where you've been and so this gives you an idea of how to track and put a pin on an item on your map now we're going to make another video that will show us how to click those buttons on the map and we could create some interaction with the map as well but for right now we got ourselves a map that shows our list so that's good progress one more video and we will show a little bit more interactivity see you soon hey welcome back to another video in our android app with gps in this video we're going to take our map and make it a little bit interactive so when we show the map of where we've placed our pins if i click a pin we can actually do something with it i'll show you how to make interaction [Music] okay so this should be a pretty quick video it's not really that complex of what we have to do so the activity that we're going to work on is the maps activity and we are going to program inside of the just the java code all right so let's um let's do two things here we're going to do a zoom in on the last pin that was dropped and then we're also going to add the ability to click on those pins so let's come into the place where we're putting the pins on the map that was on the map ready so i'm going to have a new variable and we'll call that the last location so this variable that i'm going to assign is called last location placed and for the initial value we'll just set it to whatever sydney was and then after we've got this defined i'm going to redefine it inside the loop so the last location placed is going to be important because it's where we're going to zoom in on the map and so every time we place an item in our for loop i'm going to update the last location placed as lat long which is exactly this one point all right so when i get to the end of the loop outside of the little curly bracket i want to zoom in on that last location so the method to do that is called animate camera as you can see there's several options for animate camera so i'm just going to choose animate camera now inside of there i need to have something to actually configure that so the item that i'm looking for is called a camera update factory okay so it's a factory design pattern go look up factory design patterns if you want to know what that's all about and then if you look at the options here i have the first one called the new lat long zoom and you can see that the parameter says i can have a lat a long and then a float value so the float value is the zoom value so the lat long value is pretty easy i have that already decided all right so the parameters i'm looking for inside of the new lat long zoom is the last location that was placed so that's the coordinates that we're going to and then a zoom value i think you can go from anywhere from like 2 to 20 or some range like that so i'm going to pick a medium zoom like 12 and i have to make sure that it's in float float values so let's put a 12 f this might be a good place for a constant value but let's just put in 12 for now okay i'm going to relaunch the app and see if it'll zoom in on the last location that i clicked okay the app is up and running i'll show my waypoint list and it should show empty okay so i don't have any items let's add a new waypoint and let's see if it's in the list sure enough we have something i think that's london that's what it says here anyway right here in trafalgar square and now let's go show the map so it brings up the map and sure enough it zooms in and we're right there in london perfect now let's see if we can configure a new waypoint so let's go back to grand canyon university entrance set the location okay so it's now set there close here and this should update all right so i'm just going to wait it out i'm going to see if we can get the location to update it's supposed to update okay so it takes a few minutes maybe but as you can see i now have grand canyon university so like i say in a real phone it would work better okay so it looks like i finally figured i got two different locations this one's at 33 this one's at 51. we got two different spots so let's go ahead and chose the map and the second one that i chose was in in arizona okay so if i zoom out a bit you should be able to see london as well and let's see did i have something set in london it looks like i did okay so go around your neighborhood and set these pins and they automatically should zoom in on you all right there's one more cool thing i'd like to show you it's the ability to click on a pin so let's go back and add a new item here so let's type in map dot set and you can see that there's a new option called set on marker click listener that sounds cool that means we're going to be able to use those pins as buttons so the click listener item that we're looking for is something called google map dot on marker click listener so i choose new and goo and you get the help typing ahead and there it is all right so now we have a new method called on marker click and it has a marker now the return false here is kind of a strange thing it's just something we have to add at the end i think it tells the computer or tells android that we're we haven't actually consumed this click it's it's going to be used again later i'm not quite sure what that is but we'll leave return false as is so i'm going to create a very simple routine here i'm just going to count the number of times that this button is clicked this pin is clicked you could set it to do other more interesting things but this just gives you a demo of how clicking works so we're going to utilize a property in a marker called a tag now tag is kind of a universal flexible type of field you can stick anything in it until you want you can put in an integer strings objects we're going to just use an integer so i'm going to say assign this value called clicks to be whatever was in get tag now you can see that there's a problem here it says you don't know really what get tag is but if you cast it you will now have an integer as a promise so if it doesn't work let's check on the second line to say if clicks turns out to be absolutely null i mean it didn't get work it didn't get anything the the tag is empty so if the tag is empty then let's set the value of clicks to zero after we set it to zero then let's increment it so we'll do a plus one and then we will set the tag to whatever new variable it is so it might be one it might be ten whatever how many clicks it was so the next goal is i'm going to have a toast that will tell us how many times that the marker was clicked so i will put in a string for the text that says marker and we'll get the title was clicked and then how many times it was clicked is from the tag so this should tell us a new number every time we click it all right let's run the application again and let's go ahead and set a new waypoint and let's show the map all right here is our marker and if i click it and let's see if the click works there it is so at the bottom i have a toast that says your marker was clicked three times and four times now there's all kinds of things you can do with markers you can set their colors you can change them to different styles other than these generic pins you can draw circles you can draw lines maps have a lot of options you can do heat maps you can do guided tours so we're just barely getting started on the maps and so since this is an android class i wanted to show you how to get into these different classes but look at the documentation to see what the full potential is so this extends our gps demo and allows you to add waypoints save them as a list show them as a map and navigate between a few screens so very basic of course but it's a great doorway opening to whatever apps and imagination that you can think of so good luck with your apps in android and maps you
Info
Channel: freeCodeCamp.org
Views: 90,849
Rating: undefined out of 5
Keywords:
Id: _xUcYfbtfsI
Channel Id: undefined
Length: 83min 15sec (4995 seconds)
Published: Tue Jan 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.