Beruflich Dokumente
Kultur Dokumente
Based Services
Introduction
Mobile phones portability led to services that let you
find, contextualize and map physical locations.
Map-based Activities using Google Maps,
Zoom-level, Centered location, Overlays
Location-based services (LBS)
Services that let you find device’s current location
GPS, Google’s cell-based location technology.
Geocoder
supports forward and reverse geocoding.
Using Location-Based Services
Main Elements:
LocationManager
Provides hooks to the location-based services.
LocationProviders
represents location-finding technology
Updating Locations in Emulator Location
Providers
Selecting a Location Provider
Depending on the device, there may be several
technologies that Android can use
Offer different capabilities
power consumption, monetary cost, accuracy, altitude,
speed, heading information
String providerName = LocationManager.GPS_PROVIDER;
LocationProvider gpsProvider;
gpsProvider =
locationManager.getProvider(providerName);
Finding the Available Providers
LocationManager.GPS_PROVIDER
LocationManager.NETWORK_PROVIDER
To get a list of all/only enabled providers:
boolean enabledOnly = true;
List<String> providers =
locationManager.getProviders(enabledOnly);
Finding Providers Based on
Requirement Criteria
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(true);
String bestProvider =
locationManager.getBestProvider(criteria, isEnabledOnly);
Finding Providers Based on
Requirement Criteria
If more than one => the one with the greatest accuracy
If no Location Providers => requirements, the criteria are
loosened, in the following order, until a provider is found:
Power use
Accuracy
Ability to return bearing, speed, and altitude
Cost is never implicitly relaxed.
If no provider is found, null is returned.
List<String> matchingProviders =
locationManager.getProviders(criteria, isEnabledOnly);
Finding Your Location
String serviceString = Context.LOCATION_SERVICE;
LocationManager locationManager;
locationManager =
(LocationManager)getSystemService(serviceString);
To support access to the LBS hardware.
<uses-permission android:name=“android.permission.
ACCESS_FINE_LOCATION”/>
<uses-permission android:name=“android.permission.
ACCESS_COARSE_LOCATION”/>
An application that has been granted fine permission will have
coarse permission granted implicitly.
String provider = LocationManager.GPS_PROVIDER;
Location location =
locationManager.getLastKnownLocation(provider);
Tracking Movement
String provider = LocationManager.GPS_PROVIDER;
int t = 5000; // milliseconds
int distance = 5; // meters
LocationListener myLocationListener = new
LocationListener() {
public void onLocationChanged(Location location) {
// Update application based on new location. }
public void onProviderDisabled(String provider) {
// Update application if provider disabled. }
public void onProviderEnabled(String provider) {
// Update application if provider enabled. }
Tracking Movement
public void onStatusChanged(String provider, int status,
Bundle extras){
// Update application if provider hardware status
changed.
} };
locationManager.requestLocationUpdates(provider, t,
distance, myLocationListener);
locationManager.removeUpdates(myLocationListener);
Most GPS hardware incurs significant power cost.
disable updates whenever possible, specifically when
location changes are used to update User Interface.
Using Proximity Alerts
Proximity alerts let your applications set triggers that are
fired when a user moves within or beyond a set distance
from a geographic location.
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.147536;
double lng = 0.510638;
float radius = 100; // meters
Using Proximity Alerts
long expiration = -1; // do not expire
Intent intent = new Intent(TREASURE_PROXIMITY_ALERT);
PendingIntent proximityIntent =
PendingIntent.getBroadcast(this, -1, intent, 0);
locationManager.addProximityAlert(lat, lng, radius, expiration,
proximityIntent);}