Route Tracker App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. The Route Tracker App ©1992-2013 by Pearson Education, Inc. All Rights Reserved. • Monitors user’s location and bearing (direction) • Virtually displays a route on a map (in red with black dots every 10 GPS data points received by app) • There is a Start Tracking ToggleButton • The map shifts as the user moves, so user’s location is centered on screen • Map is oriented so that route tracking line is pointed in direction the user is traveling and that direction points to top of the device • Android emulator does not emulate bearing data (in text) • User can choose Map or Satellite options • Touching map displays Google Maps street map (the default) • At Stop Tracking – displays a dialog with total distance travelled and average speed ©1992-2013 by Pearson Education, Inc. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Test Driving the App ©1992-2013 by Pearson Education, Inc. All Rights Reserved. • Import the App • Obtain a Google Maps API Key ▫ For only testing and debugging apps The ADT Plugin automatically handles getting a debug certificate Get the fingerprint value of the SDK Debug certificate (instructions at code.google.com/android/maps-apis/mapkey.html) Use the fingerprint value to get your Google Maps API key (produced at code.google.com/android/maps-api-signup.html) Replace the value of the String resource named google_maps_api_key in the strings.xml file with the Google Maps API key. ▫ For creating apps for distribution Need a fingerprint of your Signing certificate Running and Test-driving App ©1992-2013 by Pearson Education, Inc. All Rights Reserved. • On an Android device ▫ Will need Internet access on device ▫ Ensure device is set up correctly for testing and debugging apps (see Before You Begin) ▫ In Eclipse….Select Run As > Android Application Select your device in Android Device Chooser window Install app and run it ▫ To acquire GPS signal, must have line-of-sight with the GPS satellite Go outside When device recieves GPS signal, you’ll see a Toast Now you can start tracking Running and Test-driving App ©1992-2013 by Pearson Education, Inc. All Rights Reserved. • In an AVD ▫ Edit Android AVD (can use NexxusS) ▫ In the Edit AVD window, select the Google APIs) – API level # from Target list (10 for 2.3.3) ▫ Click Edit AVD ▫ Select that AVD and start it ▫ Run the App, selecting the newly edited AVD Sending GPS data to an AVD with GPX files ©1992-2013 by Pearson Education, Inc. All Rights Reserved. • No actual GPS signal • Load and “play” .gpx files • Can create these files with free app GPSLogger ▫ Produces files in GPX version 1.0 ▫ Convert them to GPX version 1.1 using a tool you can get online for the Android emulator Sending GPS data to an AVD with GPX files cont. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. • To send GPS data from .gpx file to an AVD ▫ Once app is running in AVD, in Eclipse select Window>Open Perspective>DDMS ▫ In Devices tab, select your AVD ▫ In Emulator Control tab, click the GPX tab ▫ Click the Load GPS …button, locate and select one of the files in the GPXFiles folder ▫ Click Open ▫ In bottom half of GPX tab, select the file just opened and click play button ▫ Now you can click StartTracking…StopTracking Technologies Overview ©1992-2013 by Pearson Education, Inc. All Rights Reserved. • AndroidManifest.xml file ▫ To access non-standard library (ie Google Maps API) need <uses-library> element ▫ Want most of screen for displaying map, so need to specify android:theme with a theme with no title bar ▫ To access shared Android services, need usespermission element Services that change power settings, obtain location data, control sleep When user install app, the OS will tell the user the app requires these services, and asks user to confirm granting them Technologies Overview • ToggleButton ▫ ▫ ▫ ▫ ▫ New widget! Maintains an on-off state Gray bar – off state, Green bar- on state Subclass of CompoundButton Implement interface CompoundButton.OnCheckedChangeListener ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Technologies Overview ©1992-2013 by Pearson Education, Inc. All Rights Reserved. • MapActivity, MapView and Overlay Classes ▫ All in com.google.android.maps package ▫ MapActivity is a subclass of Activity that manages a MapCiew ▫ MapView displays maps from GoogleMaps API Supports gestures to zoom and pan map Additional functionality must be added programmatically ▫ Overlay To display data on a MapView, create a subclass of Overlay Override Overlay’s Draw method Use GeoPoints to translate GPS data into points to re-center map Technologies Overview ©1992-2013 by Pearson Education, Inc. All Rights Reserved. • Location Data ▫ Package android.location contains classes and interfaces for acquiring and using location data ▫ Class LocationManager – provides access to device’s location services Provides capabilities to choose best location provider based on app’s requirements specified in Criteria object Criteria settings: accuracy, battery usage, bearing, speed, altitude, monetary cost of provider Request updates from location provider and delivered to LocationListener as Location objects ▫ Location object Latitude, longitude, time ( may have altitude and speed) Technologies Overview ©1992-2013 by Pearson Education, Inc. All Rights Reserved. • PowerManager Class ▫ ▫ ▫ ▫ Comes from package android.os Use sparingly Here, want device to track even if screen is off Acquires a WakeLock to prevent device from sleeping • Display Class ▫ Provide’s access to display’s screen dimensions – used to scale maps so they fill screen as we rotate them ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Building the GUI and Resource Files • Create new Android project called Route Tracker • Change AndroidManifest.xml file ▫ uses-library ▫ android:theme ▫ uses-permission • Delete and create a new main.xml file ▫ Use FrameLayout (stacks components, most recent on top) ▫ Add ToggleButton in bottom right ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Can be done in tab of AndroidManifest Editor ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Requires Internet access to download map and satellite images Requires precise location data to show user’s route on the map To receive mock data for testing purposes – necessary only during development, not production ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Building the App ©1992-2013 by Pearson Education, Inc. All Rights Reserved. • Three classes ▫ RouteTracker (MapActivity subclass) The main Activity RouteTracker is created when you create the project, but you must change its superclass from Activity to MapActivity in the source code Only one MapActivity per process is currently supported. ▫ BearingFrameLayout – must add this class ▫ RouteOverlay – must add this class UML Class Diagram RouteTracker:MapActivity OnCreate OnStart onStop +updateLocation isRouteDiplayed onCreateOptionsMenu onOptionsItemSelected Anonymous:LocationListener onLocationChanged onProviderDisabled onProviderDisabled onStatusChanged BearingFrameLayout:FrameLayout BearingFrameLayout +getChildLayouParams dispatchDraw +setBearing +getMapView RouteOverlay:Overlay RouteOverlay +addPoint +reset draw Anonymous:GpsStatus.Listener onGpsStatusChanged onProviderDisabled onProviderDisabled onStatusChanged Anonymous:onCheckedChangeListener onCheckedChanged Note: Class is anonymous, the object won’t be ©1992-2013 by Pearson Education, Inc. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Actually, creating FrameLayout that will have a MapView instance variable ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Adds our routeOverlay to mapView’s collection of Overlays Configuring Criteria object ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Criteria object represents the app’s requested features and settings for a location provider • setAccuracy – Criteria.ACCURACY_FINE indicates the app requires precise GPS data (uses more power) ▫ ACCURACY_COURSE and for 2.3 -> ACCURACY_HIGH, ACCURACY_MEDIUM, ACCURACY_LOW • setBearingRequired – true indicates the bearing is required / used to orient map • setCostAllowed – true indicates it’s OK for app to use data services that might incur costs Configuring Criteria object ©1992-2013 by Pearson Education, Inc. All Rights Reserved. • setPowerRequirement – location providers require different amounts of power, want least amount ▫ Criteria.POWER_LOW, .NO_REQUIREMENT, POWER_HIGH, POWER_MEDIUM • setAltitudeRequired – false indicates this is not required ©1992-2013 by Pearson Education, Inc. All Rights Reserved. For last quiz – be able to list several SystemServices used by the author ©1992-2013 by Pearson Education, Inc. All Rights Reserved. ms and distance in meters between location updates For demo purposes only. Use larger values to conserve battery. Corresponding release method ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Here it is. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. 1E6 converts it to microdegrees… …and then integers. Uses a smooth animation. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Number of degrees to the east of true north. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Anonymous inner class, instantiated object is locationListener. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. then button was toggled to stop tracking. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Otherwise it was toggled to start tracking. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Draw the tracked route on the MapView. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Called with true first to draw its shadow layer (map markers, etc.), then called again with false to draw the overlay itself. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Wrap Up ©1992-2013 by Pearson Education, Inc. All Rights Reserved. • Know the new features used in the manifest file ▫ Uses-library, android:theme, uses-permission • New widgit ToggleButton and its event handler CompoundButton.OnCheckedChangedListener • MapActivity and MapView methods • LocationManager provides access to device’s location services and chose best location provider based on settings in Criteria object • Know the settings that can be changed in a Criteria object • Know system services used in this textbook • Be able to tell me what your two most favorite concepts were that you learned in this class
© Copyright 2026 Paperzz