• For bearing (i.e., compass-style direction), hasBearing()
will tell you if there is a bearing available, and getBearing()
will return it as degrees east of true north.
• For speed, hasSpeed()
will tell you if the speed is known, and getSpeed()
will return the speed in meters per second.
A more likely approach to getting the Location
from a LocationProvider
, though, is to register for updates, as described in the next section.
On the Move
Not all location providers are necessarily immediately responsive. GPS, for example, requires activating a radio and getting a fix from the satellites before you get a location. That is why Android does not offer a getMeMyCurrentLocationNow()
method. Combine that with the fact that your users may well want their movements to be reflected in your application, and you are probably best off registering for location updates and using that as your means of getting the current location.
The Weather
and WeatherPlus
sample applications (available in the Source Code area at http://apress.com) show how to register for updates — call requestLocationUpdates()
on your LocationManager
instance. This takes four parameters:
1. The name of the location provider you wish to use
2. How long, in milliseconds, must have elapsed before we might get a location update
3. How far, in meters, the device must have moved before we might get a location update
4. A LocationListener
that will be notified of key location-related events, as shown in the following code:
LocationListener onLocationChange = new LocationListener() {
public void onLocationChanged(Location location) {
updateForecast(location);
}
public void onProviderDisabled(String provider) {
// required for interface, not used
}
public void onProviderEnabled(String provider) {
// required for interface, not used
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
// required for interface, not used
}
};
Here, all we do is call updateForecast()
with the Location
supplied to the onLocationChanged()
callback method. The updateForecast()
implementation, as shown in Chapter 30, builds a Web page with the current forecast for the location and sends a broadcast so the activity knows an update is available.
When you no longer need the updates, call removeUpdates()
with the LocationListener
you registered.
Are We There Yet? Are We There Yet? Are We There Yet?
Sometimes you want to know not where you are now, or even when you move, but when you get to where you’re going. This could be an end destination, or it could be getting to the next step on a set of directions so you can give the user the next turn.
To accomplish this, LocationManager
offers addProximityAlert()
. This registers a PendingIntent
, which will be fired off when the device gets within a certain distance of a certain location. The addProximityAlert()
method takes the following as parameters:
• The latitude and longitude of the position that you are interested in.
• A radius, specifying how close you should be to that position for the Intent to be raised.
• A duration for the registration, in milliseconds — after this period, the registration automatically lapses. A value of -1 means the registration lasts until you manually remove it via removeProximityAlert()
.
• The PendingIntent
to be raised when the device is within the “target zone” expressed by the position and radius.
Note that it is not guaranteed that you will actually receive an Intent if there is an interruption in location services or if the device is not in the target zone during the period of time the proximity alert is active. For example, if the position is off by a bit and the radius is a little too tight, the device might only skirt the edge of the target zone, or go by so quickly that the device’s location isn’t sampled while in the target zone.
It is up to you to arrange for an activity or intent receiver to respond to the Intent
you register with the proximity alert. What you then do when the Intent
arrives is up to you: set up a notification (e.g., vibrate the device), log the information to a content provider, post a message to a Web site, etc. Note that you will receive the Intent
whenever the position is sampled and you are within the target zone — not just upon entering the zone. Hence, you will get the Intent
several times, perhaps quite a few times depending on the size of the target zone and the speed of the device’s movement.
Testing… Testing…
The Android emulator does not have the ability to get a fix from GPS, triangulate your position from cell towers, or identify your location by some nearby WiFi signal. So, if you want to simulate a moving device, you will need to have some means of providing mock location data to the emulator.
For whatever reason, this particular area has undergone significant changes as Android itself has evolved. It used to be that you could provide mock location data within your application, which was very handy for demonstration purposes. Alas, those options have all been removed as of Android 1.0.
One likely option for supplying mock location data is the Dalvik Debug Monitor Service (DDMS). This is an external program, separate from the emulator, which can feed the emulator single location points or full routes to traverse, in a few different formats. DDMS is described in greater detail in Chapter 37.
CHAPTER 34
Mapping with MapView and MapActivity
One of Google’s most popular services — after search, of course — is Google Maps, where you can find everything from the nearest pizza parlor to directions from New York City to San Francisco (only 2,905 miles!) to street views and satellite imagery.
Android, not surprisingly, integrates Google Maps. There is a mapping activity available to users straight off the main Android launcher. More relevant to you, as a developer, are MapView
and MapActivity
, which allow you to integrate maps into your own applications. Not only can you display maps, control the zoom level, and allow people to pan around, but you can tie in Android’s location-based services to show where the device is and where it is going.
Fortunately, integrating basic mapping features into your Android project is fairly easy. However, there is a fair bit of power available to you, if you want to get fancy.
Terms, Not of Endearment
Google Maps, particularly when integrated into third party applications, requires agreeing to a fairly lengthy set of legal terms. These terms include clauses that you may find unpalatable.