![Page 1: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/1.jpg)
Programming the Android Platform
Location & Maps
![Page 2: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/2.jpg)
Location Services
Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired
location Searching for stores near a current
location Android allows applications to
determine & manipulate location
![Page 3: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/3.jpg)
Location
Represents a position on the Earth A Location instance consists of:
Latitude, longitude, a UTC timestamp Optionally, altitude, speed, and bearing
![Page 4: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/4.jpg)
LocationProvider
Represent 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
![Page 5: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/5.jpg)
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
![Page 6: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/6.jpg)
LocationManager
System service for accessing location data getSystemService(Context.LOCATION_SE
RVICE) Enables
Determining the last known user location Registering for location updates Registering to receive Intents when the
device nears a given location
![Page 7: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/7.jpg)
LocationListener
Defines callback methods that are called when Location or LocationProvider status changes void onLocationChanged(Location location) void onProviderDisabled(String provider) void onProviderEnabled(String provider) void onStatusChanged(String provider, int status,
Bundle extras)
![Page 8: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/8.jpg)
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
![Page 9: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/9.jpg)
Obtaining Location (cont.)public class LocationGetLocationActivity extends Activity { … public void onCreate(Bundle savedInstanceState) { … final LocationManager locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
bestReading = locationManager.
getLastKnownLocation(LocationManager.GPS_PROVIDER); … locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0,0, locationListener);
…
![Page 10: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/10.jpg)
Obtaining Location (cont.) … final LocationListener locationListener = new
LocationListener() { public synchronized void onLocationChanged(Location
location) { if (location.getAccuracy() < bestReading.getAccuracy())
{ bestReading = location; tv.setText(getDisplayString(location)); } … } ….
![Page 11: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/11.jpg)
Obtaining Location (cont.) Executors.newScheduledThreadPool(1).schedule(
new Runnable() { public void run() { locationManager.removeUpdates(locationListener); } }, 10000, TimeUnit.MILLISECONDS); …
![Page 12: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/12.jpg)
Determining Best Location
Several factors to consider Measurement time Accuracy Provider type
![Page 13: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/13.jpg)
Battery Saving Tips
Location measurement drains the battery Return updates less frequently Restrict the set of Location Providers▪ Use least accurate (cheaper) provider
possible Always check last known measurement
![Page 14: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/14.jpg)
Maps
A visual representation of area Today’s examples use Google Maps library Not part of standard Android distribution
Install SDK add-on build against add-on ("Google APIs (Google Inc.)"
Permissions <uses-library
android:name="com.google.android.maps" /> <uses-permission
android:name="android.permission.INTERNET" />
![Page 15: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/15.jpg)
Maps Classes
MapActivity MapView GeoPoint Overlay ItemizedOverlay
![Page 16: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/16.jpg)
MapActivity
Base class for Activities that display MapViews
Subclass creates MapView in onCreate()
Only one MapActivity is allowed per process
![Page 17: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/17.jpg)
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 Support overlay views Requires a Maps API key
See http://code.google.com/android/add-ons/google-apis/mapkey.html
![Page 18: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/18.jpg)
MapActivity
public class MapsEarthquakeMapActivity extends MapActivity {
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } protected boolean isRouteDisplayed() { return false; }}
![Page 19: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/19.jpg)
MapActivity (cont.)
<RelativeLayout .."><com.google.android.maps.MapView
android:id="@+id/mapview" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:clickable="true"android:apiKey=”my Maps API key"
/></RelativeLayout>
![Page 20: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/20.jpg)
GeoPoint
Represents a location on Earth latitude and longitude measured in
microdegrees 1 microdegree == 1 millionth of a
degree
![Page 21: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/21.jpg)
21
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()
![Page 22: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/22.jpg)
22
ItemizedOverlay
Subclass of Overlay Manages a list of OverlayItems
OverlayItems have a particular location Draws a drawable at OverlayItem’s
location Keeps track of a focused item
![Page 23: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/23.jpg)
EarthQuakeMapActivity
public class MapsEarthquakeMapActivity extends MapActivity { List<Overlay> mapOverlays; Drawable mOverlayDrawable; EartQuakeDataOverlay itemizedOverlay; MapView mapView = null; public void onCreate(Bundle savedInstanceState) { … new HttpGetTask() .execute(
"http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo” );
… mapView …. mapView.setBuiltInZoomControls(true); mapOverlays = mapView.getOverlays(); mOverlayDrawable = this.getResources()
.getDrawable(R.drawable.pushpin_red); itemizedOverlay = new EartQuakeDataOverlay(mOverlayDrawable);}
![Page 24: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/24.jpg)
EarthQuakeMapActivity (cont.) … // called when HttpGet().execute() finishes private void onFinishGetRequest(List<EarthQuakeRec>
result) { for (EarthQuakeRec rec : result) { itemizedOverlay.addOverlay(new
OverlayItem(rec.getGeoPoint(), String.valueOf(rec.getMagnitude()),
"")); } mapOverlays.add(itemizedOverlay); MapController mc = mapView.getController(); mc.setCenter(new GeoPoint((int) (14.6041667 * 1E6),
(int) (120.9822222 * 1E6))); }
![Page 25: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/25.jpg)
EarthQuakeMapActivity (cont.)public class EartQuakeDataOverlay extends
ItemizedOverlay<OverlayItem> {
ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
protected EartQuakeDataOverlay(Drawable defaultMarker) { super(boundCenter(defaultMarker)); } public void addOverlay(OverlayItem overlay) { mOverlays.add(overlay); populate(); } protected OverlayItem createItem(int i) { return
mOverlays.get(i); } public int size() { return mOverlays.size(); }}
![Page 26: Location & Maps. Mobile applications can benefit from being location-aware, e.g., Routing from a current to a desired location Searching for stores](https://reader036.vdocuments.us/reader036/viewer/2022062421/56649d2f5503460f94a06e1b/html5/thumbnails/26.jpg)
Lab Assignment