Skip to main content

Adding the Device's Current Location to the Map

Lesson 32 from: Developing Android Apps with Java

Tony Hillerson

Adding the Device's Current Location to the Map

Lesson 32 from: Developing Android Apps with Java

Tony Hillerson

buy this class

$00

$00
Sale Ends Soon!

starting under

$13/month*

Unlock this classplus 2200+ more >

Lesson Info

32. Adding the Device's Current Location to the Map

Lessons

Class Trailer

Day 1

1

Overview: What is Android?

19:56
2

Eclipse and SDK setup and build a Red Flashlight app

38:05
3

Ad a button to a Green Flashlight

21:06
4

Add the Green Flashlight

18:48

Day 2

5

Week 1 - Layout and Build a Task Manager App

04:10
6

Displaying a list of tasks

40:44

Lesson Info

Adding the Device's Current Location to the Map

um, let's go through a really quick exercise of adding the current users or that the devices location, um, to the map so that we could see it on the map when we want to add a location to a task were nearby. And we could just do it now or or whatever, but but it is a least interesting to see. So, um, to do that, we have to actually have a location. And when the emulator starts out, it doesn't have a location. Um, and that's a good way to talk about the one part of the DDM s, um, perspective of eclipse, which is something you could also something you can also do from the command line. But in eclipse, you can get a little gooey. For doing this. You can actually send it send locations or ah, or a latte long pairs to the device and make it spoof a location. In fact, you can even give it a list of either K m, l or G P X, which is ah, a couple different formats for displaying a list of locations, which is K Mills, Google's format, and it it, uh, you could drive Google Earth, for instance, off...

of ah camel like trip and show have exhumed to different locations in this list of locations. But you could also power the emulator by doing the same thing. We'll be talking about that in just a little bit. But also, if you just need to show a location, you can start out by by using the defaults that DDM s gives you, which is actually, interestingly enough, nearby. The 1600 um, 16 are amphitheatre of it. It's pronounced based on however I was spelling it amphitheater way, um, which is near Google's headquarters. So, um, we'll see how that starts out right away. It starts out without without a location, and we send this location, it will show up on the mapas as our current location. So the steps we have to go through are we want to add Ah, Mama, my like my location overlay to the maps overlay. And then we want to think about conserving battery life because whenever you're talking about dealing with the devices location, um, that's a separate component of the physical device. There's a couple different components, and you always want to think about what you're doing that may fire these components up and make them start working. One is network access Whenever you access the network, Um, over Thea over the air, out in the out in the world, not on WiFi. You're using one radio. And when you're using WiFi using another radio, um, and then we're using using GPS using yet another radio on each one of those has power needs. And so it's gonna drain battery life. The more that you're using those and then more frequently, that you're using them. The more free, the more frequently the users getting mad at your application for using a pure battery life. I've learned this the hard way in the past. Um especially on, like a location where absolutely break it that really needed up to date location a lot. We have to be pretty cognizant of figuring out how toe do whatever we can to shut shut off the location updates whenever we can to make sure that the battery life is is ah, it's low. I rather better life usage is low, so we're gonna go through 11 little step t to make sure that that's, um, it's going to give you an idea of how that best works here by shutting off the location updates and on pause so that we don't have those hanging around in the background using a battery life. So let's go back to the ad location map activity where we're gonna add this overlay, and I think that we will do this around here. We pull that up, opened here in my offscreen code, so the first thing we want to do is add a field here. We added my location overly. It's pretty simple. Um, it's ah, comes all pretty much ready to work. You can import it from com Google Android maps, and now we have that field we will inside set of views free 81 of those so hit control on patrol on Windows commanded Mac O and go to set of views. And now, once we've got the map for you, then we can add a my location overlay um, created from this constructor, which takes the context on a map for you. And then, just like we went through last week on the map view, we want to inside the overlays of the map. We want to add another overlay and in this case writing my location overlay. And then we need to invalidate the map for you. Since we've got it all set up eso that it will redraw and let's see, I think that's all we need to do to actually get it to display. Um, but then we also want to, um when we map a current address, we are also dealing with the overlays. We get the map, you ah, list of overlays right here. And then we add an overlay that we create the address overlay. But we also want to make sure that the my location overlay isn't there. Um, in that case. And now here's the other piece is that I was talking about, um underneath on create. I'm gonna put in two other lifecycle methods on resume and on pause. We've already talked about on resuming a previous class, but once again, on resume is called, um, after on create and also after a a activity that has already displayed and already created, um is again resumed from from from we're coming back from another activity. Basically. So we went forward to another activity and then click finish somehow or hit the back button, we would come back to this. This activity and on create would already be called. It wouldn't get called again, but on resume would. So on resume is where you can do things that you want to happen, regardless of if the activity has already been run. And in this case, we want to enable my location on the my location overlay. Um, and then on pause will be called when we navigate away from this apple This activity and we want to disable the location on the my location that really in that place and that will deep down in the energy, we'll see how this works, actually, and mawr dealing, dealing more explicitly with things. Um, like the location manager. But, um, the my location overlay will deal with the location manager itself deep in there. So this will This will turn off the location, updates a to write places. So let's go see if that gets shown up on the map. Start this over. And when we go to add tasks and we at a location to a task, we'll show up on the map and it will show up with no location overlay on the reason is that it doesn't have a location. So what we can do is come to the D. M s perspective. You could go to this this view right here in this little button and say open the DDM s perspective. That should show up for you. And again, we've seen that a couple times. So you get a list of devices you get, um, uh, access to all the threads they're running. If there any threads that are running File Explorer, like we looked at some interesting things like that we could see that are the task manager is running right here, and there's this process I d. We also get the emulator control where weaken do a little bit of, um, kind of simulating real life conditions on the emulator and the thing that we want to do. If you scroll down here to the location controls, just just open this up. Full screen location controls will allow us to spoof a location on the emulator, and it already had already comes pre filled out with one at this longitude latitude like like send that will show up on the device. And we should have I mean, he didn't have to get away and come back. Should have a location. The reason that's not showing up. The reason that none of this is working right now and you're structures embarrassed is because I forgot to update the manifest one more time. So that's what you tell us every week? Yep. Always. So all this was planned. All this was totally planned. Why did you help me when I needed it? Okay. Any case, Um, there is another permission we need to add to the manifest, and I'm going to copy that in right now. So, underneath the the Internet, we also need to indicate to the user that we want to access their location or their devices location. And there are two different levels that you can get. One is access the course location, which I'm not sure of the actual resolution, but it'll get to like, I don't know, maybe a mile or something like that. I mean, around a mile, Probably not exactly a mile. Some sort of meteor equivalent. Um, 5000 or some some number of meters, I guess, and then find location will go down to the finest level available, which hopefully is the GPS, um, GPS given location, which should be within, you know, a little bit less than 10 meters, which is pretty pretty accurate. So once these permissions are set up, um, then and just to be sure that everything deploys correctly, I'm gonna hop out and actually on install the application because I've had problems in the past where I've changed something about, um the permissions, Um, especially having to do with the maps. And it didn't seem like the application. Pick those up. Something on installed. He's just to be safe, and then we'll redeploy the application, and we should have our location overlays showing up. So if anybody else gets into this situation or you're frustrated and not something's not working the way you expected, um, make sure you check your manifest. Okay? Starting from scratch. Is it Google location to task? Ah, look at this. You see this up here? Um, which had some cardamom, some sort of pointer where my mouse is now. I've got an indicator that I'm getting GPS. Um, I'm getting some sort of location data. That's the little indicator guy right here in the top. And there is the my location overlay. And if I go to and fifth theater in Mountain View and that will zoom into that location and we'll see that were fairly close. In fact, spot on with that same address. And that's the inside the DDM s perspective one more time. That's the latte along that that is defaulted inside of there, which is Google headquarters. Okay, so to sum up, if you don't see the location, you need to add those things to you manifest. So, Tony, somebody actually asked why? We need to add both the fine and the course if just specified. Fine. Would you get the fine or and you wouldn't get the course? Um, yeah, I just added them to show the examples. If you if you specified fine, you would get Actually, that's a good question. Is it? I mean, I assume it would, uh, give you course as well, But I'm not sure. Like if if fine is just GPS and courses is ah WiFi strangulation or something like that or ah, cell tower, triangulation or WiFi? I don't know. Um, I guess I mean, if you're gonna ask for fine, it's not a big deal. to ask for course as well, so you could just throw em both in there. And maybe that's a topic for the forum. Weaken. Discuss whether or not course comes along with fine or not go. Okay. Um, also, there's this, um, there's still some little walkie nous with the emulator. You can see my my location over. They went away because And even if I zoom out, I won't see it anymore because the emulator lost the location, which happens sometimes, too. So you just may have to go spam that location send button inside the DD mess sometimes. But in any case, I'll add this to our list of tasks that we have a location or a task with the location. And then it will add one without, so I get us back to the state were at before I I forgot to add those essential manifest pieces. And now are there any questions about what we just did? Um, you see nothing specifically about what we just did. We do have a couple of other questions related to maps that I'm saving for the end. Ruth just really briefly access. Fine location is equal to the GPS. granularity. Is that right? I think, um, well, I think it's generalized a bit, so it doesn't have to be GPS. Maybe there's some other way of getting fine location that we come up with the future in the future? I don't know, but yeah, it's It's pretty much, um it gives you GPS hole. Ah, and I think course level at this point gives you either WiFi or cell tower triangulation, which is still fairly accurate. You know, we may get within, I don't know. 200 meters or so on the cell tower on WiFi to WiFi may even be better than that, but yeah, I think that's I think that's how it breaks down. Okay, Um, so do we need to We need the enable my location call inside map. Kurt Address after all. Or can we remove it again? Somebody was asking that. Ah, Good question. Um, let's just move on and weaken. Call that we can figure that out later. I'm not sure. You know, basically, we're asking if I need to add this line. I'll get rid of its for the next time we let me run it. Sorry. Go ahead with questions and then why No kind of security exception Before manifest was updated for GPS, they were surprised that there wasn't some kind of security exception to let you know that she hadn't updated the manifest. I believe, um, that sounds like a good bug to file with Google is if I'm understanding the question right. You're asking why didn't get some sort of ah, on exception or something when I tried to access location, but I didn't specify the permission. Yeah, Um well, I think that there's their policy to just let those fail silently because of the wise. Um, I don't know. You could get in a situation where you have a confusing error message, although it would really help the development effort. So maybe I don't know if there's a way they could. They could help out developers in my position more often, guys that forget to update the manifest

Ratings and Reviews

a Creativelive Student
 

I thought everything on this site was free and now they want 99 dollars for a course

a Creativelive Student
 

very good

Student Work

RELATED ARTICLES

RELATED ARTICLES