Skip to content

SL4A LocationFacade

michael edited this page Nov 9, 2015 · 2 revisions

This facade exposes the LocationManager related functionality.

Overview
Once activated by 'startLocating' the LocationFacade attempts to return location data collected via GPS or the cell network. If neither are available the last known location may be retrieved. If both are available the format of the returned data is:
{u'network': {u'altitude': 0, u'provider': u'network', u'longitude': -0.38509020000000002, u'time': 1297079691231L, u'latitude': 52.410557300000001, u'speed': 0, u'accuracy': 75}, u'gps': {u'altitude': 51, u'provider': u'gps', u'longitude': -0.38537094593048096, u'time': 1297079709000L, u'latitude': 52.41076922416687, u'speed': 0, u'accuracy': 24}}
If neither are available {} is returned.
Example (python):
 import android, time
 droid = android.Android()
 droid.startLocating()
 time.sleep(15)
 loc = droid.readLocation().result
 if loc = {}:
   loc = getLastKnownLocation().result
 if loc != {}:
   try:
     n = loc['gps']
   except KeyError:
     n = loc['network'] 
   la = n['latitude'] 
   lo = n['longitude']
   address = droid.geocode(la, lo).result
 droid.stopLocating()
 

The address format is:
[{u'thoroughfare': u'Some Street', u'locality': u'Some Town', u'sub_admin_area': u'Some Borough', u'admin_area': u'Some City', u'feature_name': u'House Numbers', u'country_code': u'GB', u'country_name': u'United Kingdom', u'postal_code': u'ST1 1'}]

geocode Returns a list of addresses for the given latitude and longitude.
latitude (Double)
longitude (Double)
maxResults (Integer) maximum number of results (default=1)
returns: (List) A list of addresses.
getLastKnownLocation Returns the last known location of the device.
returns: (Map) A map of location information by provider.
locationProviderEnabled Ask if provider is enabled
provider (String) Name of location provider
locationProviders Returns availables providers on the phone
readLocation Returns the current location as indicated by all available providers.
returns: (Map) A map of location information by provider.
startLocating Starts collecting location data.
minDistance (Integer) minimum time between updates in milliseconds (default=60000)
minUpdateDistance (Integer) minimum distance between updates in meters (default=30)
stopLocating Stops collecting location data.
Clone this wiki locally