Updating Your Location in Where Am I

In the following example, the Where Am I? project is enhanced to track your current location by listening for location changes. Updates are restricted to one every two seconds, and only when movement of more than 10 meters has been detected.

Rather than explicitly selecting the GPS provider, in this example you'll create a set of Criteria and let Android choose the best provider available.

1. Start by opening the WhereAmI Activity in the Where Am I? project. Update the onCreate method to find the best Location Provider that features high accuracy and draws as little power as possible.

@Override public void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState); setContentView(R.layout.main);

LocationManager locationManager;

String context = Context.LOCATION_SERVICE;

locationManager = (LocationManager)getSystemService(context);

Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE);

Prepared for ASHLEE KABAT, email: [email protected] Order number: 56760408 This PDF is for the purchaser's personal use in accordance with the Wrox Terms of Service and under US copyright as stated on this book's copyright page. If you did not purchase this copy, please visit www.wrox.com to purchase your own copy.

criteria.setAltitudeRequired(false);

criteria.setBearingRequired(false);

criteria.setCostAllowed(true);

criteria.setPowerRequirement(Criteria.POWER_LOW);

String provider = locationManager.getBestProvider(criteria, true);

Location location = locationManager.getLastKnownLocation(provider); updateWithNewLocation(location);

2. Create a new LocationListener instance variable that fires the existing updateWithNew Location method whenever a location change is detected.

private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { updateWithNewLocation(location);

public void onProviderDisabled(String provider){ updateWithNewLocation(null);

public void onProviderEnabled(String provider){ } public void onStatusChanged(String provider, int status,

Bundle extras){ }

3. Return to onCreate and execute requestLocationUpdates, passing in the new Location Listener object. It should listen for location changes every two seconds but fire only when it detects movement of more than 10 meters.

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

LocationManager locationManager;

String context = Context.LOCATION_SERVICE;

locationManager = (LocationManager)getSystemService(context);

Criteria criteria = new Criteria();

criteria.setAccuracy(Criteria.ACCURACY_FINE);

criteria.setAltitudeRequired(false);

criteria.setBearingRequired(false);

criteria.setCostAllowed(true);

criteria.setPowerRequirement(Criteria.POWER_LOW);

String provider = locationManager.getBestProvider(criteria, true);

Location location =

locationManager.getLastKnownLocation(provider); updateWithNewLocation(location);

locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);

If you run the application and start changing the device location, you will see the Text View update accordingly.

All code snippets in this example are part of the Chapter 8 Where Am I? project, available for download at Wrox.com.

USING PROXIMITY ALERTS

It's often useful to have your applications react when a user moves toward, or away from, a specific location. Proximity alerts let your applications set triggers that are fired when a user moves within or beyond a set distance from a geographic location.

Internally, Android may use different Location Providers depending on how close you are to the outside edge of your target area. This allows the power use and cost to be minimized when the alert is unlikely to be fired based on your distance from the target area interface.

To set a proximity alert for a given coverage area, select the center point (using longitude and latitude values), a radius around that point, and an expiry time-out for the alert. The alert will fire if the device crosses over that boundary, both when it moves from outside to within the radius, and when it moves from inside to beyond it.

When triggered, proximity alerts fire Intents, most commonly broadcast Intents. To specify the Intent to fire, you use a PendingIntent, a class that wraps an Intent in a kind of method pointer, as shown in the following code snippet:

Intent intent = new Intent(MY_ACTION);

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, -1, intent, 0);

The following example sets a proximity alert that never expires and that is triggered when the device moves within 10 meters of its target:

private static String TREASURE_PROXIMITY_ALERT = "com.paad.treasurealert";

private void setProximityAlert() {

String locService = Context.LOCATION_SERVICE; LocationManager locationManager;

locationManager = (LocationManager)getSystemService(locService);

double lat = 73.14753 6;

double lng = 0.510638;

float radius = 100f; // meters long expiration = -1; // do not expire

Intent intent = new Intent(TREASURE_PROXIMITY_ALERT);

Pendinglntent proximitylntent = PendingIntent.getBroadcast(this, -1, intent, 0);

locationManager.addProximityAlert(lat, lng, radius, expiration, proximitylntent);

When the Location Manager detects that you have crossed the radius boundary —that is, you have moved either from outside to within or from inside to beyond the specified proximity radius —the packaged Intent will be fired with an extra keyed as LocationManager.KEY_PROXIMITY_ENTERING set to true or false accordingly.

To handle proximity alerts you need to create a BroadcastReceiver, such as the one shown in Listing 8-3.

LISTING 8-3: Creating a proximity alert Broadcast Receiver public class ProximityIntentReceiver extends BroadcastReceiver { ©Override public void onReceive (Context context, Intent intent) { String key = LocationManager.KEY_PROXIMITY_ENTERING;

Boolean entering = intent.getBooleanExtra(key, false); [ ... perform proximity alert actions ... ]

To start listening for proximity alerts, register your receiver:

IntentFilter filter = new IntentFilter(TREASURE_PROXIMITY_ALERT); registerReceiver(new ProximityIntentReceiver(), filter);

USING THE GEOCODER

Geocoding lets you translate between street addresses and longitude/latitude map coordinates. This can give you a recognizable context for the locations and coordinates used in location-based services and map-based Activities.

The geocoding lookups are done on the server, so your applications will require you to include an Internet uses-permission in your manifest, as shown here:

<uses-permission android:name="android.permission.INTERNET"/>

The Geocoder class provides access to two geocoding functions:

> Forward geocoding Finds the latitude and longitude of an address

> Reverse geocoding Finds the street address for a given latitude and longitude

The results from these calls are contextualized by means of a locale (used to define your usual location and language). The following snippet shows how you set the locale when creating your Geocoder. If you don't specify a locale, it will assume your device's default.

Geocoder geocoder = new Geocoder(getApplicationContext(),

Locale.getDefault());

Both geocoding functions return a list of Address objects. Each list can contain several possible results, up to a limit you specify when making the call.

Each Address object is populated with as much detail as the Geocoder was able to resolve. This can include the latitude, longitude, phone number, and increasingly granular address details from country to street and house number.

Geocoder lookups are performed synchronously, so they will block the calling thread. For sloiv data connections, this can lead to a Force Close dialog. In most cases it's good form to move these lookups into a Service or background thread, as demonstrated in Chapter 9.

For clarity and brevity, the calls made in the code samples within this chapter are made on the main application thread.

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment