Location-aware apps - Hello App Inventor!: Android programming for kids and the rest of us (2015)

Hello App Inventor!: Android programming for kids and the rest of us (2015)

Chapter 13. Location-aware apps

In this chapter, we’ll be looking at the fascinating subject of how your phone knows where it is, what you might want to do with that information, and how you would use this in an app.

Perhaps you already know that a global positioning system (GPS) has something to do with satellites and location. In fact, GPS is a system of 24 satellites that orbit at a speed of 3.9 km per second, sending out a signal of their position. You can “see” at least four of these satellites from anywhere on Earth. Your phone uses their signals alongside a lot of math to work out your precise location. In this chapter, we’ll explain how you can use GPS data to show latitude, longitude, and even street addresses.

The GPS system was first used by the US Navy and went on to be used by other members of the international armed forces as well as emergency services and then civil engineers to enable them to measure geographical areas more precisely. Of course, now every new mobile phone has the ability to access GPS, which (among other things) can be used for fun stuff like getting to know a new city using suggested information and images from the internet (such as apps like Layar and Around Me), or using a GPS-enabled exercise app to track your cycle rides or runs and see if you’re getting fitter and faster (apps like MapMyRide and Nike+).

Your phone contains a location sensor that can determine your latitude, your longitude, and where you are. The App Inventor component called LocationSensor (in the Sensors Palette group) lets you use the information picked up by the phone’s location sensor.

In this chapter, you’ll design two adventurous apps using GPS technology.

Learning Point: Latitude and longitude

The globe of course is round(ish!), so to pinpoint exactly where we are on Earth, a system was developed that measures how far up you are (latitude) and how far around you are (longitude). The zero line for latitude is an imaginary line called the equator that runs around the “center” of the Earth. As you get closer to the North Pole, you have a bigger and bigger positive latitude (up to 180); and as you get closer to the South Pole, you have a bigger and bigger negative latitude (to -180).

The zero line for longitude is in Greenwich (near London, England). As you head east from Greenwich, the longitude goes from 0 to 180; and as you head west, it goes negative as far as -180.

All other coordinates are measured in relation to these two fixed points. A good way to remember which way around these are is that “LA”titude is the vertical one, like “AL”titude.

The following diagram shows a flattened-out globe that gives a rough approximation of the latitude and longitude of all places. What is the location with 0 latitude and 0 longitude? Can you estimate the latitude and longitude of where you live? When you make the following app and find out your exact latitude and longitude, you can check if you were right.

Lost & Found app

PURPOSE OF THIS APP

This app lets you find out exactly where you are (latitude, longitude, and address) with the click of a button.

APP RATING

ASSETS YOU’LL NEED

Map image.

If you get the design of the screen right, the Components panel should look like this.

As you aren’t storing any data, making any lists, or doing too much, this whole clever little app can be achieved in one event handler. The following single step shows how you get the button’s Click event handler and call the readings from the location sensor that tell you the address, latitude, and longitude of your location. Job done!

This app displays the latitude and longitude using GPS and then finds the street address by connecting to Google Maps online. So, you’ll need an internet connection in order to see the address; the latitude and longitude will display even without an internet connection. You need a Wi-Fi or phone internet connection for any location-aware app that uses the current address or links out to Google Maps (like the next two apps).

That’s it for this app. GPS apps don’t work on the emulator, but you can use it to check how the layout looks; then link the app to your phone and try it.

Learning Point: GPS settings

You need a clear view of the sky to get a reading from the GPS satellites. But if you’re indoors and don’t have a view of the sky, you can still get a good reading through your Wi-Fi or mobile phone network. Have GPS enabled on your phone, select Use Wireless Networks, and then select from your phone’s choices.

Remember that when you started this app, we suggested that you use the grid to estimate your latitude and longitude and then check it with Lost & Found? Were you right? We were close!

Taking it further

Apps can be presents! Why not make the Lost & Found app for someone you know with a photo of them as the image instead of the map?

Homing Pigeon app

PURPOSE OF THIS APP

This app lets you find out exactly where you are (address, latitude and longitude address). Then if you’re on a school trip, for example, you can record this meeting point location, go off exploring, and with the press of another button be directed back to a meeting point.

APP RATING

ASSETS YOU’LL NEED

Homingpigeonimage.png, foldedmap.png.

This app has quite a few components, but it’s worth the effort. We’ve included a screen shot of the Components Palette that you can use as a great checklist to make sure that once you’ve put in all the hard work, you get paid back with an app that does something pretty clever!

As we said earlier, GPS apps don’t work on the emulator; but while you’re designing Homing Pigeon, you can have the emulator open to check that the layout looks right.

ActivityStarter

In chapter 12, we looked at the ActivityStarter component in detail. But we thought it might be useful to zero in on its properties in this app to make sure you get it just right. When you drag the ActivityStarter component onto the screen, you have to make sure you copy theAction, ActivityClass, and ActivityPackage exactly as shown here, including lowercase and uppercase:

Action: android.intent.action.VIEW

ActivityClass: com.google.android.maps.MapsActivity

ActivityPackage: com.google.android.apps.maps

Layout of the screen

TableArrangement1 and TableArrangement2 are laid out as follows:

CurrentAddressTitle

CurrentAddresslabel

(Leave empty)

(Leave empty)

CurrentGPSLabel

CurrentLatLabel

CurrentCommaLabel

CurrentLongLabel

Due to the use of table arrangements, the design of this app is a bit fiddly. This diagram, used with the screen grabs at right of the Components Palette and the app, should help you understand how everything lines up.

Coding the blocks

To code the Homing Pigeon app, you’ll be dealing with five different blocks and event handlers. First, drag out the LocationSensor1.LocationChanged event handler, which takes data from the Android location sensor and displays it in the labels you’ve set up. Next you code theRememberCurrentLocation-Button.Click event handler, which saves the data from the current location into the TinyDB database in your phone (we talked about TinyDB in chapter 11). Then code the GetDirectionsButton.Click event handler to take the data from the remembered location and the new current location and search Google Maps for a route back to the Homing Pigeon meeting point. As with many apps, you need to set a starting point, or baseline, when the app is first opened; and in this case, you need to put in a Screen1.Initializeblock that checks to see if there is a remembered address before the user attempts to find their way back to the Homing Pigeon point.

1. Recording the starting location

Using the LocationSensor block, you populate the current address, latitude, and longitude labels with data. This means as soon as the app opens, without the user even needing to click a button, the sensor realizes that the user is in a new place and puts that information inCurrentAddressLabel and the other labels.

2. The Remember Current Location button

In this step, you use the event handler RememberCurrentLocationButton.Click that means something will happen when the user clicks the button. You’ll set the remembered address, latitude, and longitude label text to the current address details. This means you’re setting the location to get back to: your meeting point.

3. Saving the location to Tiny DB, and activating the directions button

Next you want to save the location into Tiny DB. We looked at this in detail in chapter 11, and you know Tiny DB enables the user to save data to the mobile device for future use. In this case, the user is taking the current location information, displaying it as the remembered location, and saving it to Tiny DB so that once the user changes location, they can find their way back to their first destination.

4. The Get Directions button

In order to get directions to the remembered location, you need to use an activity starter. As you know from the Book Finder app in chapter 10, the ActivityStarter component launches other apps. In this case, you’re creating blocks that put together a search that launches Google Maps using the two locations: current and remembered.

For simplicity, imagine that the current location has latitude 0 and longitude 1 and the remembered location has latitude 2 and longitude 3. The whole URL would be

http://maps.google.com/maps?saddr=0,1&daddr=2,3

Aren’t make Text blocks brilliant? You can mix text with data and search for anything!

5. Creating the tempaddress variable

You’ve already done some work with variables earlier in the book. But to remind you, sometimes you need to create a variable that hasn’t already been created by a component. In this case, you need to create a variable called tempaddress. When the user opens the app, this variable’s purpose is to test whether a location is already saved as the remembered location. It’ll become clearer in a minute.

6. Initializing the app

For some apps, this one included, you need to do some checks as the screen opens. In this case, you need to put some blocks together to check whether the user has already saved a location in TinyDB. Then, if they have saved a location, you want to populate the remembered location table with the address, latitude, and longitude.

7. The if block

To complete the Screen1.Initialize block, you insert an if statement. The if statement checks whether there is an address in TinyDB. There will of course be an address in TinyDB only if the user previously saved their current location. The next step the user will probably want to take is to find their way back to this remembered location, so the Screen1.Initialize block ends by enabling the GetDirections button.

Taking it further

1. You could extend Homing Pigeon by letting users add locations to a list so they can choose which location to return to, rather than having just one.

2. Improve the interface of Homing Pigeon by generating better buttons. Use a site like Button Maker (www.grsites.com/generate/group/2000/).

What did you learn?

In this chapter, you learned about the following:

· How phones know where they are

· What latitude and longitude are

· How to use the location sensor

· How to access Google Maps in an app using the activity starter

Test your knowledge

1

Why are make Text blocks so useful?

2

Where is the 0,0 location?

3

What is the imaginary line around the center of the globe called?

4

How many GPS satellites are there?

5

Who used the GPS system first?

6

How do you check if there is anything in a variable when you initialize an app?

Taking it further

Try creating a different sort of QR treasure hunt that enables the user to use the location sensor.