180 likes | 292 Views
Cosc 5/4730. GPS/Location android.location. Simulator notes. All the simulators can simulator GPS/location information Android DDMS commands (geo) to the emulator. Android basics. Get a LocationManger from the system Choose a provider with Criteria or just a provider
E N D
Cosc 5/4730 GPS/Location android.location
Simulator notes • All the simulators can simulator GPS/location information • Android • DDMS • commands (geo) to the emulator.
Android basics • Get a LocationManger from the system • Choose a provider with Criteria or just a provider • providers: gps, network, etc… • get the Location and use the data. • Use a LocationListener
Permissions • Add them to the AndroidManifest.xml <uses-permission android:name="android.permission.INTERNET" /> • Maybe needed for some things, but needed the mapactivity. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> • GPS location <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> • Cell_ID or WiFI location • There maybe other needed as well • ACCESS_LOCATION_EXTRA_COMMANDS, ACCESS_MOCK_LOCATION • See http://developer.android.com/intl/zh-CN/reference/android/Manifest.permission.html for all permissions.
android.location package • Address • A class representing an Address, i.e, a set of Strings describing a location. • Criteria • A class indicating the application criteria for selecting a location provider. • Geocoder • A class for handling geocoding and reverse geocoding. • GpsSatellite • This class represents the current state of a GPS satellite. • GpsStatus • This class represents the current state of the GPS engine. • Location • A class representing a geographic location sensed at a particular time (a "fix"). • LocationManager • This class provides access to the system location services. • LocationProvider • An abstract superclass for location providers.
android.location package (2) • Listeners • GpsStatus.Listener • Used for receiving notifications when GPS status has changed. • GpsStatus.NmeaListener • Used for receiving NMEA sentences from the GPS. • LocationListener • Used for receiving notifications from the LocationManager when the location has changed. • A addProximityAlert(double latitude, double longitude, float radius, long expiration, PendingIntent intent) can be added to the LocationManger • Sets a proximity alert for the location given by the position (latitude, longitude) and the given radius.
LocationManager • This is the factory class to get location information • You do not instantiate this class directly, retrieve it through Context.getSystemService(Context.LOCATION_SERVICE) • Now you can get a Location information with getLastKnownLocation(String Provider); • and more information about the provider with LocationProvidergetProvider(String name) • Providers are found in a couple of ways
Providers • List<string> getProviders(Boolean enabledOnly) • get a list of providers, true for ones that are working • String getBestProvider(Criteria criteria, booleanenabledOnly) • get a provider based on Criteria • List<string> getAllProviders() • Returns a list of all providers • You can use booleanisProvidersEnabled(String) to determine if it is enabled or not.
LocationProvider • Information about the Provider • intgetAccuracy(), intgetPowerRequirement(), booleanhasMonetaryCost() • booleanmeetsCriteria(Criteria criteria) • booleanrequiresCell() • Returns true if the provider requires access to an appropriate cellular network (e.g., to make use of cell tower IDs), false otherwise. • booleanrequiresNetwork() • Returns true if the provider requires access to a data network (e.g., the Internet), false otherwise. • booleanrequiresSatellite() • Returns true if the provider requires access to a satellite-based positioning system (e.g., GPS), false otherwise. • booleansupportsAltitude() • Returns true if the provider is able to provide altitude information, false otherwise. • booleansupportsBearing() • Returns true if the provider is able to provide bearing information, false otherwise. • booleansupportsSpeed() • Returns true if the provider is able to provide speed information, false otherwise.
Criteria Note: NO_REQUIREMENT, POWER_USAGE_LOW, POWER_USAGE_MEDIUM, POWER_USAGE_HIGH are field constants
Location • Has a standard set of getters you would expect • double getLatitude(), double getLongitude(), float getSpeed(), double getAltitude() • long getTime() • Returns the UTC time of this fix, in milliseconds since January 1, 1970. • float getAccuracy() • Returns the accuracy of the fix in meters. • float getBearing() • Returns the direction of travel in degrees East of true North. • Returns true if has X • hasAltitude(), hasBearing(), hasSpeed(), hasAccuracy()
Location (2) • float bearingTo(Location dest) • Returns the approximate initial bearing in degrees East of true North when traveling along the shortest path between this location and the given location. • static void distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results) • Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them. • float distanceTo(Location dest) • Returns the approximate distance in meters between this location and the given location.
Example code get a location LocationManagermyL = (LocationManager) getBaseContext().getSystemService(Context.LOCATION_SERVICE); //or use (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE); Location loc = myL.getLastKnownLocation("gps"); if (loc != null ) { double sLatitude = loc.getLatitude(); double sLongitude = loc.getLongitude(); String location = sLatitude+","+sLongitude; } else { //No location can be found with gps }
LocationListener • Create a LocationListener or implement LocationListener in a class • The following methods must be implemented • void onLocationChanged(Location location) • Called when the location has changed. • void onProviderDisabled(String provider) • Called when the provider is disabled by the user. • void onProviderEnabled(String provider) • Called when the provider is enabled by the user. • void onStatusChanged(String provider, int status, Bundle extras) • Called when the provider status changes. • Status can be OUT_OF_SERVICE, TEMPORARILY_UNAVAILABLE, AVAILABLE
LocationListener • added to your LocationManger • LocationManagermyL = (LocationManager) getBaseContext().getSystemService(Context.LOCATION_SERVICE); • myL.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocationListener); • here Use a GPS provider, could be NETWORK_PROVIDER • minTime the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value. Here set to 0 • minDistance the minimum distance interval for notifications, in meters. Again set to 0 here. • myLocationListener is a the LocationListener to be called.
Example code • A simple android program is provided on the website. It will display location information in a TextView. It also has a LocationListener. • Use the ddms.bat in the tools directory to change the location info.
References • Android (many links have mapactivity as well, skipped in lecture, we’ll come back to it) • http://developer.android.com/intl/zh-CN/guide/topics/location/index.html • http://foo.jasonhudgins.com/2007/12/cruising-around-with-android.html • http://www.androidcompetencycenter.com/2009/01/android-location-api/http://www.vogella.de/articles/Android/article.html#locationapi • http://www.damonkohler.com/2009/02/android-recipes.html • Controlling the android emulator • http://developer.android.com/intl/zh-CN/guide/developing/tools/ddms.html#emulator-control
Q A &