Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS499 – Mobile Application Development Fall 2013 Location & Maps.

Similar presentations


Presentation on theme: "CS499 – Mobile Application Development Fall 2013 Location & Maps."— Presentation transcript:

1 CS499 – Mobile Application Development Fall 2013 Location & Maps

2 Location Services Mobile applications can benefit from being location-aware, e.g., – Routing from current to desired location – Searching for store near location Android allows application to determine & manipuate location

3 Location Represents a position on Earth A location instance consists of: – Latitude, longitude, a UTC timestamp – Optionally, altitude, speed, bearing

4 LocationProvider Represents sources of location data Actual data may come from – GPS satellites – Cell phone towers – Internet Different LocationProviders will exhibit different tradeoffs between cost, accuracy, availability & timeliness

5 LocationProvider Types Passive – Returns locations generated by other providers – Requires android.permission.ACCESS_FINE_LOCATION Network – Determines location based on cell tower and WiFi access points – Requires either android.permission.ACCESS_COARSE_LOCATION or android.permission.ACCESS_FINE_LOCATION GPS – Determines location using satellites – Requires android.permission.ACCESS_FINE_LOCATION

6 LocationManager System service for accessing location data – getSystemService(Context.LOCATION_SERVICE) Enables – Determining the last known user location – Registering for location updates – Registering to receive intents when the device nears a location

7 LocationListener Defines callback methods that are called when Location or LocatinProvider status changes – void onLocationChanged(Location location) – void onProviderDisabled(String provider) – void onProviderEnabled(String provider) – void onStatusChanged(String provider, int status, Bundle extras)

8 Obtaining Location Start listening for updates from location providers Maintain a “current best estimate” of location Stop listening for location updates Use best location estimate.

9 Location App public class LocationActivity extends Activity { private static final long TWO_MIN = 1000 * 60 * 2, MEASURE_TIME = 1000 * 30; private TextView mTextView = null; private Location mBestReading = null; private LocationManager mLocationManager = null; private LocationListener mLocationListener = null; … onCreate(); @Override protected void onDestroy() { mLocationManager.removeUpdates(mLocationListener); super.onDestroy(); } private String getDisplayString(Location location) { return "Accuracy:" + location.getAccuracy() + " Long.:" + location.getLongitude() + " Lat.:" + location.getLatitude() + " Time:" + location.getTime(); }

10 onCreate() @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTextView = (TextView) findViewById(R.id.textView1); int mTextViewColor = mTextView.getCurrentTextColor(); mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); mBestReading = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (null != mBestReading) { mBestReading.setAccuracy(Float.MAX_VALUE); if (System.currentTimeMillis() - mBestReading.getTime() > TWO_MIN) { mTextView.setTextColor(Color.RED); } mTextView.setText(getDisplayString(mBestReading)); mTextView.setTextColor(mTextViewColor); } else { mTextView.setText("No Initial Reading Available"); } // Location Listener (next slide) mLocationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mLocationListener); mLocationManager.requestLocationUpdates(LocationManager.GPS _PROVIDER, 0, 0, mLocationListener); Executors.newScheduledThreadPool(1).schedule(new Runnable() { public void run() { System.out.println("cancelled location updates"); mLocationManager.removeUpdates(mLocationListener); } }, MEASURE_TIME, TimeUnit.MILLISECONDS); }

11 mLocationListener = new LocationListener() { public synchronized void onLocationChanged(Location location) { if (null == mBestReading || mBestReading.getTime() - System.currentTimeMillis() > TWO_MIN || location.getAccuracy() < mBestReading.getAccuracy()) { mBestReading = location; mTextView.setText(getDisplayString(location)); } public void onStatusChanged(String provider, int status, Bundle extras) {} public void onProviderEnabled(String provider) {} public void onProviderDisabled(String provider) {} };

12 Determining Best Location Several factors to consider: – Measurement time – Accuracy – Provider type

13 Battery Saving Tips Location measurement drains the battery – Return updates less frequently – Restrict set of Location providers: Use least accurate (cheapest) provider possible – Always check last known measurement – Remove updates on onPause()

14 Maps Visual representation of area Going to use Google Maps library Note part of standard Android distribution – Install SDK add-on – build against add-on “Google APIs (Google Inc.)” Permissions: –

15 Maps Classes MapActivity MapView GeoPoint Overlay ItemizedOverlay

16 MapView Extends ViewGroup Displays a Map in one of several modes: – Street View – photographs – Satellite View – aerial – Traffic View – real-time traffic superimposed Supports panning and zooming Supports overlay views Requires a Maps API key – http://code.google.com/android/add-ons/google-apis/mapkey.html

17 MapActivity Base class for Activities that display MapViews Subclass creates MapView in onCreate() Only one MapActivity is allowed per process public class MapsEarthquakeMapActivity extends MapActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } protected boolean isRouteDisplayed() { return false; } <com.google.android.maps.MapView android:id="@+id/mapview“ android:layout_width="fill_parent“ android:layout_height="fill_parent" android:clickable="true“ android:apiKey=”myMapsAPIkey“ />

18 GeoPoint Represents a location on Earth – Latitude and longitude measured in microdegrees – 1 microdegree == 1 millionth of a degree

19 Overlay Manages information drawn over a map – e.g. points of interest within a given city MapView maintains a list of overlays – Retrieve via MapView.getOverlays()

20 ItemizedOverlay subclass of Overlay Manages a list of OverlayItems – OverlayItems have a particular locaiton – Draws a drawable at OverlayItem’s location Keeps track of a focused item


Download ppt "CS499 – Mobile Application Development Fall 2013 Location & Maps."

Similar presentations


Ads by Google