location. gps global positioning system – at least 4 satellites typically used 3 required extra...

21
Location

Upload: jordan-singleton

Post on 05-Jan-2016

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within

Location

Page 2: Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within

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

Page 3: Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within

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

Page 4: Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within

Components of Location in Android

• Permissions within Manifest file• LocationManager class• Location class• LocationListener interface

Page 5: Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within

Permissions within Manifest file

Page 6: Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within

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

Page 7: Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within

LocationManager class

Page 8: Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within

LocationManager

• Concrete class• Gives access to LBS services and classes• Not instantiated directly– instantiated through the given context’s

getSystemService() methodLocationManager lm =

(LocationManager)getSystemService(Context.LOCATION_SERVICE);

Page 9: Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within

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)

Page 10: Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within

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);

Page 11: Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within

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);

Page 12: Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within

Location class

Page 13: Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within

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

Page 14: Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within

Location class

• important methods– getters for data• getAltitude()• getAccuracy()• etc.

Page 15: Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within

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}

Page 16: Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within

LocationListener interface

Page 17: Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within

LocationListener

• Interface• Notified by LocationManager if status changes• LocationManager must register with the

LocationListener

Page 18: Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within

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

Page 19: Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within

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()

Page 20: Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within

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

Page 21: Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within

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