App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks...

103
App Development for Smart Devices CS 495/595 - Fall 2013 Tamer Nadeem Dept. of Computer Science Lec #6: Location and Maps

Transcript of App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks...

Page 1: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

App Development for Smart Devices � �

CS 495/595 - Fall 2013 �

Tamer Nadeem �Dept. of Computer Science�

Lec #6: Location and Maps �

Page 2: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 2 Fall 2013 CS 495/595 - App Development for Smart Devices

• Android Location � Google Maps External Library • MapViews � Location Services

• Overlays

• Student Presentations – A survey of mobile phone sensing

•  Presenter: Ben Pitts

– How Long to Wait?: Predicting Bus Arrival Time with Mobile Phone based Participatory Sensing

•  Presenter: Nidal Al-Yamani

– SurroundSense: Mobile Phone Localization Via Ambience Fingerpriting

•  Presenter: Craig McIlwee

Objective

Page 3: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 3 Fall 2012 CS 495/595 - App Development for Smart Devices

Location Services

Page 4: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 4 Fall 2013 CS 495/595 - App Development for Smart Devices

Location Service

•  Two main LBS elements – Location Manager Provides hooks to the location-based services – Location Providers Each of these represents a different location- finding technology used to determine the device’s current location •  Location Manager

– Obtain current location – Track movement – Set proximity alerts for areas – Find available Location Providers

• Location Providers

– Various location-finding technologies (GPS, Cellular network)

Page 5: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 5 Fall 2013 CS 495/595 - App Development for Smart Devices

Android Software Stack

Sensor Manager Location Manager

Page 6: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 6 Fall 2013 CS 495/595 - App Development for Smart Devices

Global Positioning System (GPS)

--- Miami1795 km --- Caracas 1874 km --- Bogota 1251 km San Jose, CR

Page 7: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 7 Fall 2013 CS 495/595 - App Development for Smart Devices

Cell Tower Triangulation

An alternative method to determine the location of a cell phone is to estimate its distance to three nearby cell towers. Distance of the phone to each antenna could be estimated based upon the lag time between the moment the tower sends a ping to the phone and receives the answering ping back. Quite similar to the 2D-Trilateration Method.

Reference: http://searchengineland.com/cell-phone-triangulation-accuracy-is-all-over-the-map-14790

Page 8: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 8 Fall 2013 CS 495/595 - App Development for Smart Devices

•  Latitude in GPS-Decimal notation: +90.00000 (North) to -90.000000 (South)

•  Longitude GPS-Decimal notation: +180.000000 (East) to -180.000000 (West)

Latitude & Longitude

Page 9: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 9 Fall 2013 CS 495/595 - App Development for Smart Devices

Latitude & Longitude

Page 10: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 10 Fall 2013 CS 495/595 - App Development for Smart Devices

Android Location Classes

GPS is the most common location provider on the Android based phones. It offers the most accuracy. Picture: Epson Infineon GPS (2.8 x 2.9mm)

The Android API provides Location data based on a variety of methods including: Cell Tower Triangulation, and most commonly GPS chip readings.

Reference: http://gizmodo.com/5152146/

Page 11: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 11 Fall 2013 CS 495/595 - App Development for Smart Devices

Android Location Classes

Page 12: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 12 Fall 2013 CS 495/595 - App Development for Smart Devices

•  A class representing a geographic location sensed at a particular time.

•  A location consists of a latitude and longitude, a UTC timestamp and optionally information on altitude, speed, and bearing.

•  Information specific to a particular provider or class of providers may be communicated to the application using getExtras, which returns a Bundle of key/value pairs.

•  Each provider will only provide those entries for which information is available.

Location Class

Page 13: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 13 Fall 2013 CS 495/595 - App Development for Smart Devices

The three common formats:

There are sixty seconds in a minute (60" = 1') and There are sixty minutes in a degree (60' = 1°). Examples:

DDD° MM' SS.S” 32° 18' 23.1" N 122° 36' 52.5" W

DDD° MM.MMM’ 32° 18.385' N 122° 36.875' W

DDD.DDDDD° 32.30642° N 122.61458° W or +32.30642, -122.61458

Location Values Format

Page 14: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 14 Fall 2013 CS 495/595 - App Development for Smart Devices

This class provides access to the system location services.

These services allow applications

1.  To obtain periodic updates of the device's geographical location,

2.  or to fire an application-specified Intent when the device enters the proximity of a given geographical location.

Location Manager

String service_name = Context.LOCATION_SERVICE; LocationManager locationManager = (LocationManager) getSystemService(service_name)

Page 15: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 15 Fall 2013 CS 495/595 - App Development for Smart Devices

Location Manager’s Methods

Page 16: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 16 Fall 2013 CS 495/595 - App Development for Smart Devices

LocationProvider Class

•  An abstract superclass for location providers.

•  A location provider supplies periodic reports on the geographical location of the device.

•  Each provider has a set of criteria under which it may be used; for example, •  some providers require GPS hardware and visibility to a number of

satellites; •  others require the use of the cellular radio, •  or access to a specific carrier's network, •  or access to the internet.

•  They may also have different battery consumption characteristics or monetary costs to the user.

•  The Criteria class allows providers to be selected based on user-specified criteria.

Page 17: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 17 Fall 2013 CS 495/595 - App Development for Smart Devices

LocationProvider’s Methods

Page 18: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 18 Fall 2013 CS 495/595 - App Development for Smart Devices

• Provider Reference

LocationProvider Class

String providerName = LocationManager.GPS_PROVIDER; LocationProvider gpsProvider; gpsProvider = locationManager.getProvider(providerName);

•  Common Location Providers: •  LocationManager.GPS_PROVIDER •  LocationManager.NETWORK_PROVIDER

boolean enabledOnly = true; List<String> providers = locationManager.getProviders(enabledOnly);

• Getting list of all providers

Page 19: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 19 Fall 2013 CS 495/595 - App Development for Smart Devices

• Provider with specific requirements

Finding Location Providers Using Criteria

Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_COARSE); criteria.setPowerRequirement(Criteria.POWER_LOW); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setSpeedRequired(false); criteria.setCostAllowed(true);

String bestProvider = locationManager.getBestProvider(criteria, true);

• To get all matching Providers

List<String> matchingProviders = locationManager.getProviders(criteria, false);

Page 20: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 20 Fall 2013 CS 495/595 - App Development for Smart Devices

LocationListener Class

Used for receiving notifications from the LocationManager when the location has changed. These methods are called if the LocationListener has been registered with the location manager service using the method:

requestLocationUpdates (Provider, minTime, minDistance, LocationListener)

Page 21: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 21 Fall 2013 CS 495/595 - App Development for Smart Devices

LocationListener’s Methods

Page 22: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 22 Fall 2013 CS 495/595 - App Development for Smart Devices

String provider = LocationManager.GPS_PROVIDER; int t = 5000; // milliseconds int distance = 5; // meters LocationListener myLocationListener = new LocationListener() { public void onLocationChanged(Location location) {

// Update application based on new location. } public void onProviderDisabled(String provider){

// Update application if provider disabled. } public void onProviderEnabled(String provider){

// Update application if provider enabled. } public void onStatusChanged(String provider, int status, Bundle extras){

// Update application if provider hardware status changed. } }; locationManager.requestLocationUpdates(provider, t, distance, myLocationListener);

LocationListener

Page 23: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 23 Fall 2013 CS 495/595 - App Development for Smart Devices

• Reference Location Manager

FINDING YOUR LOCATION

• Permissions in Manifest

• Last location “fix”

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

String service_name = Context.LOCATION_SERVICE; LocationManager locationManager = (LocationManager) getSystemService(service_name)

String provider = LocationManager.GPS_PROVIDER; Location location = locationManager.getLastKnownLocation(provider);

Page 24: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 24 Fall 2013 CS 495/595 - App Development for Smart Devices

•  In this example we request GPS services and display latitude and longitude values on the UI.

•  Notes

1.  Observe the GPS chip is not a synchronous device that will immediately respond to a “give me a GPS reading” call.

2.  In order to engineer a good solution that takes into account the potential delays in obtaining location data we place the UI in the main activity and the request for location call in a background service.

3.  Remember the service runs in the same process space as the main activity, therefore for the sake of responsiveness we must place the logic for location data request in a separate parallel thread.

Example – Obtain Location from GPS

Page 25: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 25 Fall 2013 CS 495/595 - App Development for Smart Devices

Example – Obtain Location from GPS

Page 26: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 26 Fall 2012 CS 495/595 - App Development for Smart Devices

Geocoding

Page 27: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 27 Fall 2013 CS 495/595 - App Development for Smart Devices

•  Geocoding lets you translate between street addresses and longitude/latitude map coordinates.

•  The geocoding lookups are done on the server, so your applications will require you to include an Internet uses-permission in your manifest, as shown here:

Geocoding

•  The Geocoder class provides access to two geocoding functions:

➤ Forward geocoding Finds the latitude and longitude of an address

➤ Reverse geocoding Finds the street address for a given latitude and longitude

<uses-permission android:name="android.permission.INTERNET"/>

For more details: http://developer.android.com/reference/android/location/Geocoder.html http://developer.android.com/reference/android/location/Address.html

Page 28: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 28 Fall 2013 CS 495/595 - App Development for Smart Devices

Reverse Geocoding

location = locationManager.getLastKnownLocation (LocationManager.GPS_PROVIDER);

double latitude = location.getLatitude(); double longitude = location.getLongitude(); List<Address> addresses = null; Geocoder gc = new Geocoder(this, Locale.getDefault()); try { addresses = gc.getFromLocation(latitude,

longitude, 10); } catch (IOException e) {}

Geocoder gc= new Geocoder(context, Locale.US); List<Address> streets = gc.getFromLocation(latitude, longitude, 1);

Page 29: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 29 Fall 2013 CS 495/595 - App Development for Smart Devices

Forward Geocoding

Geocoder fwdGeocoder = new Geocoder(this, Locale.US); String streetAddress = "160 Riverside Drive,

New York, New York"; List<Address> locations = null; try { locations = fwdGeocoder.getFromLocationName(streetAddress, 10); } catch (IOException e) {}

Geocoder gc= new Geocoder(this); // get decimal coordinates for up to 5 (best) matching locations List<Address> lstFoundAddresses= gc.getFromLocationName(txtStreetAddress, 5);

Page 30: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 30 Fall 2012 CS 495/595 - App Development for Smart Devices

Emulating Locations

Page 31: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 31 Fall 2013 CS 495/595 - App Development for Smart Devices

Emulating GPS Location Use Eclipse’s DDMS > Emulator Control

Keyhole Markup Language

Page 32: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 32 Fall 2012 CS 495/595 - App Development for Smart Devices

Google Maps

Page 33: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 33 Fall 2013 CS 495/595 - App Development for Smart Devices

Google Maps External Library

•  Android uses the Google Maps External Library to add mapping capabilities to your applications.

•  Google Maps External Library includes the com.google.android.maps package. The classes of this package offer built-in downloading, rendering, and caching of Maps tiles, as well as a variety of display options and controls.

•  The key class in the Maps package is com.google.android.maps.MapView, a subclass of ViewGroup.

•  The MapView provides an ideal user interface option for presenting geographical data.

Road View

Page 34: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 34 Fall 2013 CS 495/595 - App Development for Smart Devices

•  MapViews support annotation using Overlays and by pinning Views to geographical locations.

•  The Maps external library is not part of the standard Android library, so it may not be present on some compliant Android-powered devices.

•  By default the Android SDK includes the Google APIs add-on, which in turn includes the Maps external library.

•  MapViews offer full programmatic control of the map display, letting you control the zoom, location, and display modes — including the option to display satellite, street, and traffic views.

MapViews

Aerial View

Page 35: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 35 Fall 2013 CS 495/595 - App Development for Smart Devices

•  MapView is the Map View control.

•  MapActivity is the base class you extend to create a new Activity that can include a Map View. MapActivity handles the application life cycle and background service management required for displaying maps. Map Views are used only within MapActivity-derived Activities.

•  MapController is used to control the map, enabling you to set the center location and zoom levels.

•  Overlay is the class used to annotate your maps.

•  MyLocationOverlay is a special Overlay that can be used to display the current position and orientation of the device.

•  ItemizedOverlays and OverlayItems are used together to let you create a layer of map markers, displayed using Drawables and associated text.

Google Map Classes

Page 36: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 36 Fall 2013 CS 495/595 - App Development for Smart Devices

•  Manifest XML

Creating a Map-Based Activity

<uses-library android:name="com.google.android.maps"/> <uses-permission android:name="android.permission.INTERNET"/>

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <com.google.android.maps.MapView

android:id="@+id/map_view" android:layout_width="fill_parent" android:layout_height="fill_parent" android:enabled="true" android:clickable="true" android:apiKey="mymapapikey"

/> </LinearLayout>

•  Layout

Page 37: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 37 Fall 2013 CS 495/595 - App Development for Smart Devices

MapActivity import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; import android.os.Bundle; public class MyMapActivity extends MapActivity { private MapView mapView; private MapController mapController; @Override public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState); setContentView(R.layout.map_layout); mapView = (MapView)findViewById(R.id.map_view);

} @Override protected boolean isRouteDisplayed() {

// IMPORTANT: This method must return true if your Activity // is displaying driving directions. Otherwise return false. return false;

} }

Page 38: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 38 Fall 2013 CS 495/595 - App Development for Smart Devices

• Specifying how the map is displayed.

Configuring and Using Map Views

mapView.setSatellite(true); mapView.setStreetView(true); mapView.setTraffic(true);

• Querying the Map View. int maxZoom = mapView.getMaxZoomLevel(); GeoPoint center = mapView.getMapCenter(); int latSpan = mapView.getLatitudeSpan(); int longSpan = mapView.getLongitudeSpan();

• Optionally display the standard map zoom controls mapView.setBuiltInZoomControls(true);

Reference: http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/MapView.html

Page 39: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 39 Fall 2013 CS 495/595 - App Development for Smart Devices

• Use the Map Controller to pan and zoom a MapView.

• getController get a reference to a MapView’s controller.

Using the Map Controller

MapController mapController = mapView.getController();

• Map locations are represented by GeoPoint objects. Double lat = 37.422006*1E6; Double lng = -122.084095*1E6; GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());

• Re-center and zoom the Map. mapController.setCenter(point); mapController.setZoom(1); // 1=widest (or most distant), 21=tightest (nearest) view

•  ‘‘jump’’ to a new location mapController.animateTo(point);

Page 40: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 40 Fall 2013 CS 495/595 - App Development for Smart Devices

• Overlays enable you to add annotations and click handling to MapViews.

• Each Overlay lets you draw 2D primitives, including text, lines, images, etc.

• All the Overlays assigned to a Map View are added as layers, with newer layers potentially obscuring older ones.

• User clicks are passed through the stack until they are either handled by an Overlay or registered as clicks on the Map View itself.

Creating and Using Overlays

Reference: http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/Overlay.html

Page 41: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 41 Fall 2013 CS 495/595 - App Development for Smart Devices

•  To add a new Overlay create a new class that extends Overlay.

•  Override the draw method to draw the annotations you want to add, and override onTap to react to user clicks

Creating New Overlays

import android.graphics.Canvas; import com.google.android.maps.MapView; import com.google.android.maps.Overlay;

public class MyOverlay extends Overlay { @Override public void draw(Canvas canvas, MapView mapView, boolean shadow) { if (shadow == false) {

//[ . . . Draw annotations on main map layer . . . ] } else {

//[ . . . Draw annotations on the shadow layer . . . ] } } @Override public boolean onTap(GeoPoint point, MapView mapView) { // Return true if screen tap is handled by this overlay return false; } }

Page 42: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 42 Fall 2013 CS 495/595 - App Development for Smart Devices

•  The Projection class lets you translate between latitude/longitude coordinates (stored as GeoPoints) and x/y screen pixel coordinates (stored as Points).

•  A map’s Projection may change between subsequent calls to draw, so it’s good practice to get a new instance each time.

Projections

Projection projection = mapView.getProjection();

Point myPoint = new Point();

// To screen coordinates projection.toPixels(geoPoint, myPoint);

// To GeoPoint location coordinates projection.fromPixels(myPoint.x, myPoint.y);

•  Use the fromPixel and toPixel methods to translate from GeoPoints to Points and vice versa.

Page 43: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 43 Fall 2013 CS 495/595 - App Development for Smart Devices

Drawing on the Overlay Canvas @Override public void draw(Canvas canvas, MapView mapView, boolean shadow) { Projection projection = mapView.getProjection(); Double lat = -31.960906*1E6; Double lng = 115.844822*1E6; GeoPoint geoPoint = new GeoPoint(lat.intValue(), lng.intValue());

if (shadow == false) { Point myPoint = new Point(); projection.toPixels(geoPoint, myPoint);

// Create and setup your paint brush Paint paint = new Paint(); paint.setARGB(250, 255, 0, 0); paint.setAntiAlias(true); paint.setFakeBoldText(true);

// Create the circle int rad = 5; RectF oval = new RectF(myPoint.x-rad, myPoint.y-rad, myPoint.x+rad, myPoint.y+rad);

// Draw on the canvas canvas.drawOval(oval, paint); canvas.drawText("Red Circle", myPoint.x+rad, myPoint.y, paint); } }

Page 44: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 44 Fall 2013 CS 495/595 - App Development for Smart Devices

•  The onTap handler receives two parameters:

➤ A GeoPoint that contains the latitude/longitude of the map location tapped

➤ The MapView that was tapped to trigger this event

Handling Map Tap Events

@Override public boolean onTap(GeoPoint point, MapView mapView) { // Perform hit test to see if this overlay is handling the click if ([ . . . perform hit test . . . ]) { //[ . . . execute on tap functionality . . . ] return true; } // If not handled return false return false; }

Page 45: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 45 Fall 2013 CS 495/595 - App Development for Smart Devices

•  Each MapView contains a list of Overlays currently displayed.

Adding and Removing Overlays

•  To add an Overlay onto a Map View, create a new instance of the Overlay and add it to the list

•  Good practice to call postInvalidate after you modify the list to update the changes on the map display

List<Overlay> overlays = mapView.getOverlays(); MyOverlay myOverlay = new MyOverlay(); overlays.add(myOverlay); mapView.postInvalidate(); projection.fromPixels(myPoint.x, myPoint.y);

List<Overlay> overlays = mapView.getOverlays();

Page 46: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 46 Fall 2013 CS 495/595 - App Development for Smart Devices

•  Special Overlay designed to show your current location and orientation on a MapView.

My Location Overlay

•  Can display both your current location (represented as a flashing blue marker) and your current orientation (shown as a compass on the map display).

List<Overlay> overlays = mapView.getOverlays(); MyLocationOverlay myLocationOverlay = new

MyLocationOverlay(this, mapView); overlays.add(myLocationOverlay);

myLocationOverlay.enableCompass(); myLocationOverlay.enableMyLocation();

Reference: http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/MyLocationOverlay.html

•  Stopping the service myLocationOverlay.disableCompass(); myLocationOverlay.disableMyLocation();

Page 47: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 47 Fall 2013 CS 495/595 - App Development for Smart Devices

•  Itemized Overlays and Overlay Items Classes •  OverlayItems are used to supply simple maker functionality to your Map Views via

the ItemizedOverlay class.

•  The ItemizedOverlay instance handles the drawing, placement, click handling, focus control, and layout optimization of each OverlayItem marker for you.

•  Extends ItemizedOverlay<OverlayItem>, override size() to return the number of markers to display and createItem() to create a new item based on the index of each marker

•  Pinning Views to the Map and Map Positions •  You can pin any View-derived object to a Map View attaching it to either a screen

position or a geographical map location.

•  Call addView() on the MapView, usually from the onCreate or onRestore methods within the MapActivity containing it. Pass in the View you want to pin and the layout parameters to use.

•  The MapView.LayoutParams parameters you pass in to addView determine how, and where, the View is added to the map.

Additional Features

Page 48: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 48 Fall 2013 CS 495/595 - App Development for Smart Devices

•  List of Intents to invoking Google applications on Android devices http://developer.android.com/guide/appendix/g-app-intents.html

Additional Features

Page 49: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 49 Fall 2013 CS 495/595 - App Development for Smart Devices

To use the Google Map service an API key is needed. The API key can obtained as follows for development/debugging application:

1) Get debug.keystore file. You can find the location to the file under “Default debug keystore” from: Windows ➪ Preferences ➪ Android ➪ build.

2) Use keytool tool to generate Certificate fingerprint (MD5). Use following command on command prompt

•  keytool -list -alias androiddebugkey -keystore <keystore_location>.keystore -storepass android -keypass android

3) Go to ‘Sign Up for the Android Maps API‘ page. Put your Certificate fingerprint (MD5) And get your API key for android GMap application.

4) Replace “mymapapikey” in MapView layout item with your API key.

Google API Key

Page 50: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 50 Fall 2012 CS 495/595 - App Development for Smart Devices

Questions?

Page 51: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 51 Fall 2013 CS 495/595 - App Development for Smart Devices

• Example 1 - Obtain Location from GPS (in slides)

• Example 2 – Hello, MapView (in slides)

• Example 3 – GeoCoder (in slides)

• Example 4 – Overlays (in slides)

• Example 5 – MyLocation (in slides)

To DO

Page 52: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 52 Fall 2012 CS 495/595 - App Development for Smart Devices

Example 1

Page 53: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 53 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 1 – Obtain Location from GPS

<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/widget32" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" > <EditText android:id="@+id/txtMsg" android:layout_width="fill_parent" android:layout_height="120px" android:textSize="12sp" > </EditText> <Button android:id="@+id/btnStopService" android:layout_width="151px" android:layout_height="wrap_content" android:text="Stop Service" > </Button> </LinearLayout>

Layout

Page 54: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 54 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 1 – Obtain Location from GPS

Manifest <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"

package=“edu.odu.cs.cs495.mappinggps" android:versionCode="1" android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true" > <activity android:name=".MyGPS" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="MyGpsService"> </service>

</application> <uses-sdk android:minSdkVersion=“8" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> </manifest>

Page 55: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 55 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 1– Obtain Location from GPS

Main Activity: MyGPS

// Request GPS location, show lat & long // Application logic and its BroadcastReceiver in the same class package edu.odu.cs.495.mappinggps; import android.app.Activity; import android.os.Bundle; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.telephony.gsm.SmsManager; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.*;

Page 56: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 56 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 1– Obtain Location from GPS

Main Activity: MyGPS

public class MyGPS extends Activity { Buttonbtn StopService; TextView txtMsg; Intent intentMyService; ComponentName service; BroadcastReceiver receiver; String GPS_FILTER= "cs495.action.GPS_LOCATION";

Page 57: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 57 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 1– Obtain Location from GPS

Main Activity: MyGPS

@Override public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main); txtMsg= (TextView) findViewById(R.id.txtMsg);

// initiate the service intentMyService= new Intent(this, MyGpsService.class); service= startService(intentMyService);

txtMsg.setText("MyGpsServicestarted -(see DDMS Log)");

// register & define filter for local listener IntentFilter mainFilter= new IntentFilter(GPS_FILTER); receiver= new MyMainLocalReceiver(); registerReceiver(receiver, mainFilter);

Page 58: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 58 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 1– Obtain Location from GPS

Main Activity: MyGPS

btnStopService= (Button) findViewById(R.id.btnStopService); btnStopService.setOnClickListener(new OnClickListener() {

public void onClick(View v) { try{ stopService(new Intent(intentMyService) );

txtMsg.setText("After stopingService: \n“ + service.getClassName()); btnStopService.setText("Finished"); btnStopService.setClickable(false); } catch(Exception e) { Log.e("MYGPS", e.getMessage() ); } }

}); }//onCreate

Page 59: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 59 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 1– Obtain Location from GPS

Main Activity: MyGPS

////////////////////////////////////////////////////////////////////// @Override public void onDestroy() {

super.onDestroy(); try{ stopService(intentMyService); unregisterReceiver(receiver); } catch(Exception e) { Log.e("MAIN-DESTROY>>>", e.getMessage() ); }

Log.e("MAIN-DESTROY>>>", "Adios");

}// onDestroy

Page 60: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 60 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 1– Obtain Location from GPS

Main Activity: MyGPS

////////////////////////////////////////////////////////////////////// // local RECEIVER private class MyMainLocalReceiver extends BroadcastReceiver{ @Override public void onReceive(Context localContext, Intent callerIntent) {

double latitude = callerIntent.getDoubleExtra("latitude",-1); double longitude = callerIntent.getDoubleExtra("longitude",-1);

Log.e("MAIN>>>", Double.toString(latitude)); Log.e("MAIN>>>", Double.toString(longitude));

String msg= " lat: “ + Double.toString(latitude) + " " + " lon: “ + Double.toString(longitude);

txtMsg.append("\n"+ msg);

Toast.makeText(this, msg, 1000).show();

} }//MyMainLocalReceiver }//MyGPS

Page 61: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 61 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 1– Obtain Location from GPS

Main Activity: MyGpsService

// This is the GPS service. Requests location updates // in a parallel thread. sends broadcast using filter. package edu.odu.cs.cs495.mappinggps; import android.app.Service; import android.content.Context; import android.content.Intent; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.IBinder; import android.os.Looper; import android.util.Log; import android.widget.Toast; public class MyGpsService extends Service { String GPS_FILTER= "cs495.action.GPS_LOCATION"; Thread triggerService; LocationManager lm; GPSListener myLocationListener; boolean isRunning= true;

Page 62: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 62 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 1– Obtain Location from GPS

Main Activity: MyGpsService

@Override public Ibinder onBind(Intent arg0) { return null; } @Override public void onCreate() { super.onCreate(); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); Log.e("<<MyGpsService-onStart>>", "I am alive-GPS!"); // we place the slow work of the service in its own thread so the // response we send our caller who run a "startService(...)" method // gets a quick OK from us.

Page 63: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 63 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 1– Obtain Location from GPS

Main Activity: MyGpsService triggerService= new Thread(newRunnable() { public void run() { try{

Looper.prepare();

// try to get your GPS location using the LOCATION.SERVIVE provider lm= (LocationManager) getSystemService(Context.LOCATION_SERVICE);

// This listener will catch and disseminate location updates myLocationListener= new GPSListener(); long minTime= 10000; // frequency update: 10 seconds float minDistance= 50; // frequency update: 50 meter lm.requestLocationUpdates( //request GPS updates LocationManager.GPS_PROVIDER, minTime, minDistance, myLocationListener);

Looper.loop();

} catch(Exception e) { Log.e("MYGPS", e.getMessage() );

} }// run }); triggerService.start(); }// onStart

Page 64: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 64 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 1– Obtain Location from GPS

Main Activity: MyGpsService

///////////////////////////////////////////////////////////////////////// // location listener becomes aware of the GPS data and sends a broadcast private class GPSListener implements LocationListener{ public void onLocationChanged(Location location) {

//capture location data sent by current provider double latitude = location.getLatitude(); double longitude = location.getLongitude();

//assemble data bundle to be broadcasted Intent myFilteredResponse= new Intent(GPS_FILTER); myFilteredResponse.putExtra("latitude", latitude); myFilteredResponse.putExtra("longitude", longitude); Log.e(">>GPS_Service<<", "Lat:" + latitude + " lon:" + longitude);

//send the location data out sendBroadcast(myFilteredResponse);

}

Page 65: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 65 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 1– Obtain Location from GPS

Main Activity: MyGpsService

public void onProviderDisabled(String provider) { } public void onProviderEnabled(String provider) { } public void onStatusChanged(String provider, int status,

Bundle extras) { } };//GPSListener class }// MyGPSService

Page 66: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 66 Fall 2012 CS 495/595 - App Development for Smart Devices

Example 2

Page 67: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 67 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 2 – Hello, MapView Create a simple Activity that can view and navigate a map.

Reference: http://developer.android.com/guide/tutorials/views/hello-mapview.html

Page 68: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 68 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 2 – Hello, MapView 1.  Start a new project/Activity called HelloMapView.

2.  Because we're using the Google Maps library, which is not a part of the standard Android library, we need to declare it in the Android Manifest. Open the AndroidManifest.xml file and add the following as a child of the <application> element:

<uses-library android:name="com.google.android.maps" />

3.  We also need access to the internet in order to retrieve the Google Maps

tiles, so the application must request the INTERNET permissions. In the manifest file, add the following as a child of the <manifest> element:

<uses-permission android:name="android.permission.INTERNET" />

Page 69: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 69 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 2 – Hello, MapView 4. Now open the main layout file for your project. Define a layout with a

com.google.android.maps.MapView inside a RelativeLayout:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mainlayout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">

<com.google.android.maps.MapView android:id="@+id/mapview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:apiKey="Your Maps API Key" />

</RelativeLayout>

Page 70: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 70 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 2 – Hello, MapView

4. cont. The clickable attribute defines whether you want to allow user-interaction with the map. In this case, we set it "true" so that the user can navigate. The apiKey attribute holds the Google Maps API Key that proves your application and signer certificate has been registered with the Google Maps service. Because MapView uses Google Maps data, this key is required in order to receive the map data, even while you are developing (see appendix A).

For the purpose of this tutorial, you should register with the fingerprint of the SDK debug certificate. Once you've acquired the Maps API Key, insert it for the apiKey value.

Page 71: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 71 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 2 – Hello, MapView

5.  Now open the HelloMapView.java file. For this Activity, we're going to extend the special sub-class of Activity called MapActivity, so change the class declaration to extend MapActicity, instead of Activity: public class HelloMapView extends MapActivity {

6.  The isRouteDisplayed() method is required, so add it inside the class:

@Override protected boolean isRouteDisplayed() { return false; }

7. Now go back to the HelloMapView class. At the top of HelloMapView,

instantiate a handles for the MapView and the Map controller.

MapView mapView; MapController controller;

Page 72: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 72 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 2 – Hello, MapView 8.  Wire-up the XML layout widget and the Java controls.

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MapView mapView;

mapView = (MapView) findViewById(R.id.mapview); mapView.setBuiltInZoomControls(true);

GeoPoint point = new GeoPoint (25800000,-80266667); // Miami City

controller = map.getController(); controller.animateTo(point); controller.setZoom(3);

}

Page 73: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 73 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 2 – Hello, MapView

9.  In the previous fragment the mapView is activated by the use of the built-

in zoom facility (new feature). This zoom control will appear at the center-bottom of the screen each time the user taps on the screen, and will disappear a few seconds later.

10.  The MapController method .animateTo(geoPoint) center the map on the given coordinates.

11.  The zoom factor range is 1..17 (17 closest to the map).

12.  Ready to run.

Page 74: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 74 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 2 – Hello, MapView

Intial map After tapping and zooming in After panning to go South

Page 75: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 75 Fall 2012 CS 495/595 - App Development for Smart Devices

Example 3

Page 76: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 76 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 3 – GeoCoder

In this example we will create an application that converts an address to its corresponding GeoPoint and displays the location on a Mapview.

In the case of multiple possible locations a list of addresses is provided

(TODO: show the list in a dialog box or list selector and allow the user to make her selection by clicking on the best choice. As an example try: “Main Ave. Ohio”

Page 77: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 77 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 3 – GeoCoder

<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" > <TextView android:id="@+id/myCaption" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Address/Coordinates" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/myAddress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" android:hint="Enter location (address)" android:textSize="18sp" />

<Button android:id="@+id/myBtnSearch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10px" android:text="Go" /> </LinearLayout> <com.google.android.maps.MapView android:id="@+id/myMap" android:apiKey="0SN3rTw6p317v08_uva72oCS_hgPTe92J2t_nwQ" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="2" android:clickable="true" /> </LinearLayout>

Layout

Page 78: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 78 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 3 – GeoCoder

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package=“edu.odu.cs.cs495.geocoder" android:versionCode="1" android:versionName="1.0"> <!-- Permissions --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-sdk android:minSdkVersion=“8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <uses-library android:name="com.google.android.maps" /> <activity android:name=".GeopointDemo1" android:label=".GeopointDemo1"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

Manifest

Page 79: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 79 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 3 – GeoCoder

// GeopointDemo1 // Enter address get location choices from a list // show MapView location from last list entry // /////////////////////////////////////////////////////////////////// package edu.odu.cs.cs495.geocoder; import java.util.List; import android.app.AlertDialog; import android.app.Dialog; import android.location.Address; import android.location.Geocoder; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView;

Page 80: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 80 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 3 – GeoCoder public class GeopointDemo1 extends MapActivity { private MapView myMap; private Button btnSearch; private EditText address; private Geocoder gc; private double lat; private double lon; protected boolean isRouteDisplayed() {

return false; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Toast.makeText(this, "Try: MAIN AVE OHIO", 1).show(); //define handle to map and attach zooming[+ -] capabilities myMap = (MapView) findViewById(R.id.myMap); myMap.setBuiltInZoomControls(true); gc = new Geocoder(this); address = (EditText) findViewById(R.id.myAddress);

Page 81: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 81 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 3 – GeoCoder btnSearch = (Button) findViewById(R.id.myBtnSearch); btnSearch.setOnClickListener(new OnClickListener() { public void onClick(View v) { String addressInput = address.getText().toString(); // Get input text try { // get up to 5 locations List<Address> lstFoundAddresses = gc.getFromLocationName( addressInput, 5); if (lstFoundAddresses.size() == 0) showInvalidAddressMsg(); else { showListOfFoundAddresses(lstFoundAddresses); //for now map the first address from the list navigateToLocation(lstFoundAddresses.get(0), myMap); } } catch (Exception e) { Toast.makeText(getBaseContext(), e.getMessage(), 1).show(); } }// onClick }); // btnSearch }// onCreate

Page 82: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 82 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 3 – GeoCoder // Navigates a given MapView to the specified Longitude and Latitude public static void navigateToLocation(Address adr, MapView map) { try {

//covert to integer representation of microdegrees double latitude = adr.getLatitude() * 1000000; double longitude = adr.getLongitude() * 1000000; // new GeoPoint to be placed on the MapView GeoPoint geoPt = new GeoPoint((int) latitude, (int) longitude); MapController mapCtrl = map.getController(); mapCtrl.animateTo(geoPt); // move map to the given point int maxZoomlevel = map.getMaxZoomLevel(); // detect maximum zoom level int zoomapCtrlhosenLevel = (int) ((maxZoomlevel + 1)/1.25); mapCtrl.setZoom(zoomapCtrlhosenLevel); // zoom at chosen level mapCtrl.setCenter(geoPt); //center the map around the given address map.setSatellite(false); // display only "normal road" mapview map.setTraffic(false); // do not show traffic info } catch (Exception e) { Log.e("ERROR>>>", e.getMessage() ); }

}// navigateTo

Page 83: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 83 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 3 – GeoCoder private void showInvalidAddressMsg() {

Dialog locationError = new AlertDialog.Builder( GeopointDemo1.this) .setIcon(0) .setTitle("Error") .setPositiveButton("OK", null) .setMessage("Sorry, your address doesn't exist.") .create(); locationError.show();

}// showInvalidAddressMsg private void showListOfFoundAddresses (List<Address> foundAddresses){

String msg = ""; for (int i = 0; i < foundAddresses.size(); ++i) {

// show results as address, Longitude and Latitude // TODO: for multiple results show a select-list, try: MAIN AVE OHIO Address a = foundAddresses.get(i); lat = a.getLatitude(); lon = a.getLongitude(); String adr = "\n" + a.getAddressLine(0) + "\n" + a.getAddressLine(1); msg += "\n" + i + " " + lat + " " + lon + adr; Toast.makeText(getApplicationContext(), msg, 1).show();

} }// showListOfFoundAddresses }//class

Page 84: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 84 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 3 – GeoCoder

Page 85: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 85 Fall 2012 CS 495/595 - App Development for Smart Devices

Example 4

Page 86: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 86 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 4 – Overlays In this example we map downtown Cleveland placing markers on important places around the city’s downtown and the Euclid Corridor. When the user taps on a marker a brief note with the name and description of the site appears, a long tap produces an invitation for a virtual tour of the site (to be done!)

Page 87: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 87 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 4 – Overlays

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <com.google.android.maps.MapView android:id="@+id/map" android:layout_width="fill_parent" android:layout_height="fill_parent" android:apiKey="0SN3rTw6p317v08_uva72oCS_hgPTe92J2t_nwQ" android:clickable="true" /> </RelativeLayout>

Page 88: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 88 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 4 – Overlays

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package=“edu.odu.cs.cs495.overlays" android:versionCode="1" android:versionName="1.0"> <!-- Permissions --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-sdk android:minSdkVersion=“8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <uses-library android:name="com.google.android.maps" /> <activity android:name="ClevelandOverlays" android:label="Cleveland Overlays"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

Page 89: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 89 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 4 – Overlays package edu.odu.cs.cs495.overlays; // Mapping CLEVELAND DOWNTOWN - OHIO // demonstrates SHORT & LONG TAP events import android.content.res.Resources.NotFoundException; import android.graphics.drawable.Drawable; import android.graphics.Canvas; import android.os.Bundle; import android.view.KeyEvent; import android.view.MotionEvent; import android.widget.Toast; import com.google.android.maps.GeoPoint; import com.google.android.maps.ItemizedOverlay; import com.google.android.maps.MapActivity; import com.google.android.maps.MapView; import com.google.android.maps.OverlayItem; import java.util.ArrayList; import java.util.List; public class ClevelandRocks extends MapActivity { // handle to the MapView private MapView map = null; //next two variables are part of a test for longPress event private long lastTouchTimeDown = -1; private long lastTouchTimeUp = -1;

Page 90: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 90 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 4 – Overlays @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try { map = (MapView) findViewById(R.id.map); // place Terminal Tower at the Center of the map map.getController().setCenter(getPoint(41.498370, -81.693883)); map.getController().setZoom(14); //range 1..21 map.setBuiltInZoomControls(true); Drawable marker = getResources().getDrawable(R.drawable.marker);//see note below marker.setBounds(0, 0,

marker.getIntrinsicWidth(), marker.getIntrinsicHeight());

map.getOverlays().add (new SitesOverlay(marker)); map.setSatellite(false); } catch (NotFoundException e) { Toast.makeText(getApplicationContext(), e.getMessage(), 1).show(); } }// onCreate

Note. You may pick any drawable marker from your SDK folder, say C:\Android\platforms\android-1.6\data\res\drawable

Page 91: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 91 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 4 – Overlays

@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_S) { map.setSatellite(!map.isSatellite()); return (true); } return (super.onKeyDown(keyCode, event)); } private GeoPoint getPoint(double lat, double lon) { return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0))); } @Override protected boolean isRouteDisplayed() { return (false); }

Page 92: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 92 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 4 – Overlays /////////////////////////////////////////////////////////////////////////////////////////////////////

private class SitesOverlay extends ItemizedOverlay<OverlayItem> {

private List<OverlayItem> items = new ArrayList<OverlayItem>(); private Drawable marker = null;

public SitesOverlay(Drawable marker) {

super(marker); this.marker = marker; items.add (new OverlayItem(getPoint(41.498370,-81.693883), "Terminal Tower", "AT the heart of the city")); items.add (new OverlayItem(getPoint(41.506052,-81.699560),

"Cleveland Browns Stadium", "Football legends since 1946")); items.add (new OverlayItem(getPoint(41.496550,-81.688198),

"Quicken Loans Arena", "Home of the Cleveland Cavaliers")); items.add (new OverlayItem(getPoint(41.495749,-81.685333),

"Progressive Field", "Cleveland Indians Home\nMajor League Baseball since 1900's")); items.add (new OverlayItem(getPoint(41.501719,-81.675140),

"Cleveland State University", "The People's University \nEngaged Learning")); items.add (new OverlayItem(getPoint(41.502088,-81.623003),

"Cleveland Clinic", "Top Hospital & Medical Research in the USA")); items.add (new OverlayItem(getPoint(41.506106,-81.609615),

"Severance Hall", "Cleveland Orchestra - Best in the World")); items.add (new OverlayItem(getPoint(41.504223,-81.608512),

"Case Western Reserve Universty", "One of the Nation's Top Universities")); items.add (new OverlayItem(getPoint(41.508968,-81.611754),

"Cleveland Museum of Art", "Most Distinguished \nOpen Museum in the World")); items.add (new OverlayItem(getPoint(41.508421,-81.695540),

"Rock & Roll Hall of Fame", "Preserving for the world \nthe history of RR music")); populate();

}

Page 93: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 93 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 4 – Overlays @Override protected OverlayItem createItem(int i) { return (items.get(i)); }

@Override public void draw(Canvas canvas, MapView mapView, boolean shadow) { super.draw(canvas, mapView, shadow); boundCenterBottom(marker); }

@Override protected boolean onTap(int i) { // if time Difference between lastTouchTimeUp & lastTouchTimeDown is: // > 1500 millisec. it was a LONG TAP // < 1500 just a NORMAL tap // on LONG TAPs we may want to show a dialog box with additional // data about item i-th such as pictures, links to web-sites, ???, etc. //--------------------------------------------------------------------- String text = "NORMAL TAP"; long pressTotalTime = lastTouchTimeUp - lastTouchTimeDown; if (pressTotalTime > 1500) { text = "LONG TAP"; }

Toast.makeText(getApplicationContext(), text + " " + pressTotalTime + " msec.\n" + items.get(i).getTitle() + "\n" + items.get(i).getSnippet(), 1).show();

return (true);

}

Page 94: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 94 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 4 – Overlays // TODO implement longPress actions (such as dialog box showing // pictures, links, ???, of selected point. @Override public boolean onTouchEvent(MotionEvent event, MapView mapView) {

//remember the initial time the user touches the screen if (event.getAction() == MotionEvent.ACTION_DOWN) { lastTouchTimeDown = event.getDownTime(); lastTouchTimeDown = System.currentTimeMillis(); }

if (event.getAction() == MotionEvent.ACTION_UP) { lastTouchTimeUp = System.currentTimeMillis(); } return super.onTouchEvent(event, mapView); }

@Override public int size() { return (items.size()); }

}// SitesOverlay ///////////////////////////////////////////////////////////////////////////////////////// }//class

Page 95: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 95 Fall 2012 CS 495/595 - App Development for Smart Devices

Example 5

Page 96: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 96 Fall 2013 CS 495/595 - App Development for Smart Devices

Example 5 – MyLocation

In this example we draw current location (and also compass) on the Map.

Page 97: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 97 Fall 2013 CS 495/595 - App Development for Smart Devices

<?xml  version="1.0"  encoding="utf-­‐8"?>  <manifest  xmlns:android="http://schemas.android.com/apk/res/android"            package="com.AndroidMapView"            android:versionCode="1"            android:versionName="1.0">        <uses-­‐sdk  android:minSdkVersion=“8"  />        <application  android:icon="@drawable/icon"  android:label="@string/app_name">          <uses-­‐library  android:name="com.google.android.maps"  />                <activity  android:name=".AndroidMapViewActivity"                                    android:label="@string/app_name">                        <intent-­‐filter>                                <action  android:name="android.intent.action.MAIN"  />                                <category  android:name="android.intent.category.LAUNCHER"  />                        </intent-­‐filter>                </activity>        </application>    <uses-­‐permission  android:name="android.permission.INTERNET"/>    <uses-­‐permission  android:name="android.permission.ACCESS_FINE_LOCATION"></uses-­‐permission>  </manifest>

•  Manifest file

Example 5 – MyLocation

Page 98: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 98 Fall 2013 CS 495/595 - App Development for Smart Devices

<?xml  version="1.0"  encoding="utf-­‐8"?>  <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"      android:orientation="vertical"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      >  <TextView      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="@string/hello"      />  <com.google.android.maps.MapView      android:id="@+id/mapview"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:clickable="true"      android:apiKey="Your  Maps  API  key  here"        />  </LinearLayout>

•  Main.xml

Example 5 – MyLocation

Page 99: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 99 Fall 2013 CS 495/595 - App Development for Smart Devices

package  edu.odu.cs.cs495.mylocation;      import  java.util.ArrayList;      import  android.graphics.Canvas;  import  android.graphics.drawable.Drawable;      import  com.google.android.maps.GeoPoint;  import  com.google.android.maps.ItemizedOverlay;  import  com.google.android.maps.MapView;  import  com.google.android.maps.OverlayItem;      public  class  MyItemizedOverlay  extends  ItemizedOverlay<OverlayItem>{      private  ArrayList<OverlayItem>  overlayItemList  =  new  ArrayList<OverlayItem>();      public  MyItemizedOverlay(Drawable  marker)  {          super(boundCenterBottom(marker));          //  TODO  Auto-­‐generated  constructor  stub              populate();  }  

•  MyItemizedOverlay.java

Example 5 – MyLocation

Page 100: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 100 Fall 2013 CS 495/595 - App Development for Smart Devices

public  void  addItem(GeoPoint  p,  String  title,  String  snippet){        OverlayItem  newItem  =  new  OverlayItem(p,  title,  snippet);        overlayItemList.add(newItem);        populate();  }      

@Override  protected  OverlayItem  createItem(int  i)  {        //  TODO  Auto-­‐generated  method  stub        return  overlayItemList.get(i);  }      

@Override  public  int  size()  {        //  TODO  Auto-­‐generated  method  stub        return  overlayItemList.size();  }      

@Override  public  void  draw(Canvas  canvas,  MapView  mapView,  boolean  shadow)  {        //  TODO  Auto-­‐generated  method  stub        super.draw(canvas,  mapView,  shadow);        //boundCenterBottom(marker);  }  

•  MyItemizedOverlay.java

Example 5 – MyLocation

Page 101: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 101 Fall 2013 CS 495/595 - App Development for Smart Devices

•  AndroidMapViewActivity.java

Example 5 – MyLocation

package  edu.odu.cs.cs495.mylocation;      import  com.google.android.maps.GeoPoint;  import  com.google.android.maps.MapActivity;  import  com.google.android.maps.MapView;  import  com.google.android.maps.MyLocationOverlay;      import  android.graphics.drawable.Drawable;  import  android.os.Bundle;      public  class  AndroidMapViewActivity  extends  MapActivity  {          MyItemizedOverlay  myItemizedOverlay  =  null;    MyLocationOverlay  myLocationOverlay  =  null;              /**  Called  when  the  activity  is  first  created.  */        @Override        public  void  onCreate(Bundle  savedInstanceState)  {                super.onCreate(savedInstanceState);                setContentView(R.layout.main);                MapView  mapView  =  (MapView)  findViewById(R.id.mapview);                mapView.setBuiltInZoomControls(true);  

Page 102: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 102 Fall 2013 CS 495/595 - App Development for Smart Devices

•  AndroidMapViewActivity.java

Example 5 – MyLocation

             Drawable  marker=getResources().getDrawable(android.R.drawable.star_big_on);                int  markerWidth  =  marker.getIntrinsicWidth();                int  markerHeight  =  marker.getIntrinsicHeight();                marker.setBounds(0,  markerHeight,  markerWidth,  0);                                myItemizedOverlay  =  new  MyItemizedOverlay(marker);                mapView.getOverlays().add(myItemizedOverlay);                                GeoPoint  myPoint1  =  new  GeoPoint(0*1000000,  0*1000000);                myItemizedOverlay.addItem(myPoint1,  "myPoint1",  "myPoint1");                GeoPoint  myPoint2  =  new  GeoPoint(50*1000000,  50*1000000);                myItemizedOverlay.addItem(myPoint2,  "myPoint2",  "myPoint2");                                myLocationOverlay  =  new  MyLocationOverlay(this,  mapView);                mapView.getOverlays().add(myLocationOverlay);                mapView.postInvalidate();            }      

 @Override    protected  boolean  isLocationDisplayed()  {      //  TODO  Auto-­‐generated  method  stub      return  false;    }  

Page 103: App Development for Smart Devicescs495/materials/Lec-06... · – Location Manager Provides hooks to the location-based services ... • The geocoding lookups are done on the server,

Page 103 Fall 2013 CS 495/595 - App Development for Smart Devices

•  AndroidMapViewActivity.java

Example 5 – MyLocation

 @Override    protected  boolean  isRouteDisplayed()  {      //  TODO  Auto-­‐generated  method  stub      return  false;    }        @Override    protected  void  onResume()  {      //  TODO  Auto-­‐generated  method  stub      super.onResume();      myLocationOverlay.enableMyLocation();      myLocationOverlay.enableCompass();    }        @Override    protected  void  onPause()  {      //  TODO  Auto-­‐generated  method  stub      super.onPause();      myLocationOverlay.disableMyLocation();      myLocationOverlay.disableCompass();    }    }