This chapter covers

■ Manipulating location properties in the emulator

■ Working with LocationProvider and LocationManager

■ Implementing and registering LocationListener

■ Understanding MapActivity and MapView

■ Using the Geocoder

A mobile device with accurate location awareness is very powerful. Combining location awareness with network data access is world changing—and this is where Android shines. Android isn't the only platform to support this capability, of course, but it is set apart somewhat by an easy-to-work-with and popular location API framework (Google Maps) and its open source nature.

From direct network queries to triangulation with cell towers and even GPS, an Android-powered device has access to different types of LocationProvider that it can utilize to access location data. Different providers supply a mix of location-related metrics including latitude and longitude, speed, bearing, and altitude.

GPS is the most common location provider you will work with on the Android platform, because it is the most accurate and powerful option. Nevertheless, some devices

may either not have a GPS receiver or a GPS signal may not be available. In those instances the Android platform provides the capability for you to fail gracefully—to query other providers when your first choice fails. You can configure which providers are available and hook into one or another through the LocationManager class.

Location awareness opens up a new world of possibilities for application development. We are just beginning to see what inventive developers can do with real-time location information and faster and more reliable network data access. In this chapter we are going to follow that nascent path and build an application that combines location awareness with data from the U.S. National Oceanic and Atmospheric Administration (NOAA).

Specifically we will be connecting to the National Data Buoy Center (NDBC) to retrieve data from buoys that are positioned around the coastline in North America (and a few NOAA ships). That's right; we said, "data from buoys." Thanks to the NOAA-NDBC system, which polls sensors on buoys and makes that data available in RSS feeds, we can retrieve data for the vicinity, based on the current location, and display condition information such as wind speed, wave height, and temperature to our users. (Although we won't cover non-location-related details in this chapter, such as using HTTP to pull the RSS feed data, the full source code for the application is available with the code download for this chapter.) This application, which we are calling Wind and Waves, has several main screens, including an Android MapActivity with a MapView. These components are used for displaying and manipulating map information, as shown in figure 11.1.

We admit that accessing buoy data has a somewhat limited audience—being important mainly for marine use cases (and in this case working only for fixed buoys in North America and several ships that can be used as worldwide data points)—but we wanted to demonstrate the broad scope of possibility here and to come up with something unique. Along with its uniqueness, we hope to make this an interesting application that exercises a great many of the Android location-related capabilities.

In addition to displaying data based on the current location, we will also use this application to create several LocationListener instances that we can use to receive updates when the user's location changes. When the location changes and the device lets our application know, we will update our MapView using an Overlay—an object that allows us to draw on top of the map.

Outside of what our buoy application requires, here we will also pull in a few samples for working with the Geocoder class. This class allows you to map between a GeoPoint (latitude and longitude) and a place (city or postal code) or address. This is a very helpful utility, so we will cover it even though we won't be using it on the high seas.

Before we begin building any of our example code, we will start with using the built-in mapping application and simulating our position within the Android emulator. This will allow us to mock our location for the emulator. After we have covered all of the emulator location-related options, we will move on to building Wind and Waves.

Google

Figure 11.1 Screens from the Wind and Waves location-aware application

Ahrfe^a Handw

CjeamorKja Los Angerea Ciwina o

Ingle^wdi Rednndo^

Corona e

Long Beach ©Anaheim o

Google

Si Ma { Los Angeles IMire.1 -f Monica Y ,'rf' 7" tJZ^^l

buoy

6uoy" vt"

buoy

Figure 11.1 Screens from the Wind and Waves location-aware application

0 0

Post a comment