20.08.2016 Views

Professional Android 4 Application Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

516 x CHAPTER 13 MAPS, GEOCODING, AND LOCATION-BASED SERVICES<br />

Most GPS systems record track-files using GPX, whereas KML is used extensively<br />

online to define geographic information. You can handwrite your own<br />

KML file or generate one by using Google Earth to find directions between two<br />

locations.<br />

All location changes applied using the DDMS location controls will be applied to the GPS receiver,<br />

which must be enabled and active.<br />

Configuring the Emulator to Test Location-Based Services<br />

The GPS values returned by getLastKnownLocation do not change unless at least one application<br />

requests location updates. As a result, when the Emulator is first started, the result returned from a<br />

call to getLastKnownLocation is likely to be null, as no application has made a request to receive<br />

location updates.<br />

Further, the techniques used to update the mock location described in the previous section are effective<br />

only when at least one application has requested location updates from the GPS.<br />

Listing 13-2 shows how to enable continuous location updates on the Emulator, allowing you to use<br />

DDMS to update the mock location within the Emulator.<br />

LISTING 13-2: Enabling the GPS provider on the Emulator<br />

locationManager.requestLocationUpdates(<br />

LocationManager.GPS_PROVIDER, 0, 0,<br />

new LocationListener() {<br />

public void onLocationChanged(Location location) {}<br />

public void onProviderDisabled(String provider) {}<br />

public void onProviderEnabled(String provider) {}<br />

public void onStatusChanged(String provider, int status,<br />

Bundle extras) {}<br />

}<br />

);<br />

code snippet PA4AD_Ch13_Location/src/MyActivity.java<br />

Note that this code effectively locks the GPS Location Provider into an on state. This is considered<br />

poor practice, as it will quickly drain the battery on a real device; therefore, this technique should<br />

only be used when testing on the Emulator.<br />

SELECTING A LOCATION PROVIDER<br />

Depending on the device, you can use several technologies to determine the current location. Each<br />

technology, available as a Location Provider, offers different capabilities — including differences in<br />

power consumption, accuracy, and the ability to determine altitude, speed, or heading information.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!