210 likes | 487 Views
Location. GPS. Global Positioning System At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within 20 ft high power consumption requires line of sight to satellites trilateration
E N D
GPS • Global Positioning System • At least 4 satellites typically used • 3 required • extra for error detection and altitude • typically accurate within 20 ft • high power consumption • requires line of sight to satellites • trilateration • intersection of spheres indicating distance from satellites
Other Location Based Services (LBS) • Network • Typically associated with mobile networks • Towers are used in place of satellites • not as accurate as GPS – typically within 200 ft • does not consume as much power • Passive • WIFI • extremely fast, no additional power usage • lower accuracy – typically within 1 mile • Google maintains a location database and uses wifi signal to determine location
Components of Location in Android • Permissions within Manifest file • LocationManager class • Location class • LocationListener interface
Permissions • uses-permission tags • children of manifest tag • often placed above application tag <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> allows access to GPS, network, or passive <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> only allows access to network
LocationManager • Concrete class • Gives access to LBS services and classes • Not instantiated directly • instantiated through the given context’s getSystemService() method LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationManager • important methods • getLastKnownLocation • argument: desired provider • getBestProvider • arguments • Criteria to use (power requirement, accuracy, bearing, speed, altitude) • usually a default Criteria object • boolean value • true – only those available and accessible (and meet criteria) • false – all providers on device (and meet criteria) • returns String indicating the best provider • getProviders • returns List<String> of all providers, with same boolean argument as getBestProvider (no criteria argument) • getAllProviders • returns List<String> of all providers (no arguments)
Sample code • Code to obtain current location LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); String bestProvider = lm.getBestProvider(new Criteria(), true); Location l = lm.getLastKnownLocation(bestProvider);
Sample code • LocationManager – methods associated with Listener • Registering a listener • arguments • provider to use • minimal time interval in milliseconds • minimal distance changed in meters • LocationListener • examples • lm.requestLocationUpdates(lm.getBestProvider(new Criteria(), true), 1000, 1000, this); • lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1000, this); • Unregistering a listener • lm.removeUpdates(this);
Location class • Concrete class • Represents geographic location • Types of data in this class: • accuracy – in meters • altitude – in meters • bearing – degrees east of true north • latitude • longitude • provider • speed – m/s • timestamp – ms since 1/1/1970
Location class • important methods • getters for data • getAltitude() • getAccuracy() • etc.
Sample code – Using current location LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); String bestProvider = lm.getBestProvider(new Criteria(), true); Location myLocation = lm.getLastKnownLocation(bestProvider); if (myLocation != null) { tvLatitude.setText(String.valueOf(myLocation.getLatitude())); …other code using the location goes here } else { …code handling inability to retrieve a location goes here }
LocationListener • Interface • Notified by LocationManager if status changes • LocationManager must register with the LocationListener
LocationListener • 4 abstract methods • onLocationChanged • parameter is the new location • onProviderDisabled and onProviderEnabled • parameter is the provider (String) • onStatusChanged • 3 parameters • provider (String) • status of provider (int) – constants from LocationProvider class • OUT_OF_SERVICE • TEMPORARILY_UNAVAILABLE • AVAILABLE • extras (Bundle) – currently the number of satellites
LocationListener • Registering/Unregistering a Manager with a Listener • methods in LocationManager class • Register • requestLocationUpdates(provider, delay, distance, listener) • provider (String) • delay between updates (int in ms) • distance (float indicating minimum distance change before update) • listener – the LocationListener • Unregister • removeUpdates(provider) • take care to utilize resources effectively • register in onResume() • unregister in onPause()
Typical Approach • The main GPS Activity extends Activity and implements LocationListener • within this activity: • 4 abstract methods from LocationListener are implemented • LocationManager and Location classes are used as needed • Handle the case where the Location is null • onResume and onPause methods are used to handle registering and unregistering the LocationListener • other Activities are used as needed
Simulating locations in emulator • DDMS (Dalvik Debug Monitor Service) • Perspective in Eclipse • Many views associated with it to monitor Android • Screen capture • Monitoring memory usage • Logcat • Mock data, calls, location • Other • In DDMS perspective, mock locations can be set in the Emulator Control view • manually enter longitude and latitude • upload .gpx or .kml files