14.01.2013 Views

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 7: Maps, Geocoding, and Location-Based Services<br />

220<br />

long expiration = -1; // do not expire<br />

Intent intent = new Intent(TREASURE_PROXIMITY_ALERT);<br />

PendingIntent proximityIntent = PendingIntent.getBroadcast(this, -1,<br />

intent,<br />

0);<br />

locationManager.addProximityAlert(lat, lng, radius, expiration,<br />

proximityIntent);}<br />

When the Location Manager detects that you have moved either within or beyond the specifi ed radius, the<br />

packaged Intent will be fi red with an extra keyed as LocationManager.KEY_PROXIMITY_ENTERING<br />

set to true or false accordingly.<br />

To handle proximity alerts, you need to create a BroadcastReceiver, such as the one shown in the<br />

following snippet:<br />

public class ProximityIntentReceiver extends BroadcastReceiver {<br />

}<br />

@Override<br />

public void onReceive (Context context, Intent intent) {<br />

String key = LocationManager.KEY_PROXIMITY_ENTERING;<br />

}<br />

Boolean entering = intent.getBooleanExtra(key, false);<br />

[ ... perform proximity alert actions ... ]<br />

To start listening for them, register your receiver, as shown in the snippet below:<br />

IntentFilter filter = new IntentFilter(TREASURE_PROXIMITY_ALERT);<br />

registerReceiver(new ProximityIntentReceiver(), filter);<br />

Using the Geocoder<br />

Geocoding lets you translate between street addresses and longitude/latitude map coordinates. This<br />

can give you a recognizable context for the locations and coordinates used in location-based services<br />

and map-based Activities.<br />

The Geocoder class provides access to two geocoding functions:<br />

❑ Forward Geocoding Finds the latitude and longitude of an address.<br />

❑ Reverse Geocoding Finds the street address for a given latitude and longitude.<br />

The results from these calls will be contextualized using a locale, where a locale is used to defi ne your<br />

usual location and language. The following snippet shows how you set the locale when creating your<br />

Geocoder. If you don’t specify a locale, it will assume your device’s default.<br />

Geocoder geocoder = new Geocoder(get<strong>Application</strong>Context(),<br />

Locale.getDefault());

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

Saved successfully!

Ooh no, something went wrong!