Presentation is loading. Please wait.

Presentation is loading. Please wait.

Location-Based Services. Objectives How to display Google Maps in your application How to control displayed maps How to perform geocoding and reverse.

Similar presentations


Presentation on theme: "Location-Based Services. Objectives How to display Google Maps in your application How to control displayed maps How to perform geocoding and reverse."— Presentation transcript:

1 Location-Based Services

2 Objectives How to display Google Maps in your application How to control displayed maps How to perform geocoding and reverse geocoding How to obtain geographical data using GPS, Cell-ID, and Wi-Fi How to monitor for a location 2

3 Location-Based Services (LBS) Customize services based on one's location Track one's location and offer additional services such as locating nearby amenities, as well as offering suggestions for route planning. How? Use Google Maps API (v2 or above) Providing: com.google.android.gms.maps.MapView (ViewGroup) com.google.android.gms.maps.MapFragment (Fragment) com.google.android.gms.maps.GoogleMap Display a map with data obtained from the Google Maps service (need Internet permission) Control the map (using the GoogleMap class) Draw a number of overlays (e.g., markers) 3

4 Configuring Development Environment Google Maps Android API distributed as part of the Google Play services SDK Need to acquire API key from Google API Console https://console.developers.google.com/ Provide the SHA1 fingerprint of the signing certificate To test use: Android device (with Google Play services) or AVD with Google APIs (Google Inc), level 17 (4.2 Jelly Bean) or above 4

5 In-Class: Hello Earth Displaying a Google map 1.Install the Google Play services SDK Tools > Android > SDK Manager [SDK tools tab] 2.Create a new project Application name: Hello Earth Company domain: cs4330.cs.utep.edu Minimum SDK: API 17 Android 4.2 (Jelly Bean) Choose Google Maps Activity 3.Follow the direction in value/google_maps_api.xml Visit the link provided to get a Google Maps API key Note the finger print of your debugging certificate and the package name Add the API key to the google_maps_key entry of the xml file 5

6 Displaying Maps Use MapFragment or MapView class* Specify API key and other permissions in manifest Also need the INTERNET permission and others 6 <meta-data android:name="com.google.android.geo.API_KEY" android:value="AIzaSyDIx1qJ2Uvds1585Wp1UiLiNZfprAfjlcU"/>... *from com.google.android.gms.maps package

7 7 In layout file <fragment android:id="@+id/mapFragment" android:layout_width=“match_parent" android:layout_height=“match_parent" class="com.google.android.gms.maps.MapFragment" /> or <com.google.android.gms.maps.MapView android:id="@+id/mapView" android:layout_width=“match_parent" android:layout_height=“match_parent" android:clickable="true"/>

8 8 In MainActivity If MapView is used, forward all the lifecycle methods such as onCreate and onResume from the Activity, e.g., protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MapView mapView = (MapView) findViewById(R.id.mapView); mapView.onCreate(savedInstanceState); //-- needed! }

9 Zooming In/Out Programmatically Use GoogleMap.animateCamera(CameraUpdate) methods GooleMap: main class of Google Maps API and entry point for all methods related to the map CameraUpdate: define a camera move 9 GoogleMap map = mapView.getMap(); map.animateCamera(CameraUpdateFactory.zoomIn()); map.animateCamera(CameraUpdateFactory.zoomOut()); map.animateCamera(CameraUpdateFactory.zoomTo(4.0)); // zoom level: 2.0 ~ 21.0 map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

10 Changing Views Map modes (or views) Map: drawing of streets and places of interest Satellite: tiles of aerial imagery with roads and names superimposed Use GoogleMap.setMapType(int) MAP_TYPE_NORMAL: basic map with roads MAP_TYPE_SATELLITE: satellite view with roads MAP_TYPE_TERRAIN: terrain view with roads GoogleMap map = mapView.getMap(); map.setMapType(MAP_TYPE_SATELLITE); 10

11 Changing Views (Cont.) Can also display color-coded traffic conditions Use GoogleMap.setTrafficEnabled(boolean) Toggle traffic layer on or off Green: smooth traffic (50 miles/hour) Yellow: moderate (25-50 miles/hour) Red: slow (25 miles/hour) 11

12 Latitude And Longitude Grid system of the earth Latitude: run horizontally, 0-90* (degree) north and south from equator (1 degree = about 69 miles) Longitude: run vertically, 0-180* east and west from Greenwich (1 degree = about 69 miles) Degrees further divided into minutes (', 1/60 deg) and seconds (", 1/60 min) E.g., UTEP CCS Building 31*46'03"N 106*30'06"W 12

13 Latitude And Longitude (Cont.) Use com.google.android.gms.model.LatLng Immutable class representing a pair of latitude and longitude, stored as double numbers in degree API LatLng(double, double) public fields: latitude and longitude 13

14 Navigating To Specific Locations Use GoogleMap.animateCamera(CameraUpdate) animateCamera(CameraUpdateFactory.newLatLng(new LatLng(x,y))); or CameraUpdateFactory.newLatLngZoom(LatLng, float) Q: Navigate to UTEP CCS Building at 31*46'03.79"N 106*30'06.26"W (31.767719, -106.501739 in degrees)? 14

15 Adding Markers Can add markers to a map to indicate places of interest How? Use GoogleMap.addMarker(MarkerOption), e.g., GoogleMap map =... // get a map. map.addMarker(new MarkerOptions().position(new LatLng(31.767719, -106.501739)).title("UTEP CCS").snippet("Has Starbucks!").icon(BitmapDescriptorFactory.defaultMarker( BitmapDescriptorFactory.HUE_AZURE))); Define a new overlay, a set of images which are displayed on top of the base map tiles; refer to: TitleOverlay GoogleMap.addTitleOverlay(TitleOverlayOptions) 15

16 Screen vs. Geo Coordinates Getting the location that was touched? To know the latitude and longitude of a location corresponding to a position on the screen E.g., to determine a location's address, a process known as "reverse How? Override the onTouchEvent(MotionEvent) of the View class to obtain the screen location Use the Projection class to translate between on screen location and geographic coordinates on the surface of the Earth (LatLng). Projection GoogleMap.getProjection() APIs: LatLng fromScreenLocation(Point) Point toScreenLocation(LatLng) 16

17 (Reverse) Geocoding Geocoding Address to location (latitude and longitude) Use android.location.Address class Reverse geocoding Location to address Use android.location.Geocoder class 17 double Address.getLatitude() double Address.getLongiude() addresses.get(0).getLatitude() addresses.get(0).getLongitude() List getFromLocation(double, double, int) List getFromLocationName(String, int) List addresses = geoCoder.getFromLocation(latitude, longitude, 1); // 1: no. of results

18 Getting Location Data Several ways to identify your locations: GPS, cell tower triangulation, and Wi-Fi Use android.location.LocationManager class, providing access to the system location service Need permissions such as: android.permission.ACCESS_FINE_LOCATION android.permission.ACCESS_COARSE_LOCATION 18

19 19 lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, // minTime interval for notification, in miliseconds 0, // minDistance distance between location updates, in meters new LocationListener() {... }}; Several variations requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)... LocationListener interface void onLocationChanged(Location) void onProviderDisabled(String) void onProviderEnabled(String) void onStatusChanged(String, int, Bundle)

20 In-Class (Pair): Are We There? Trace the current location by placing a marker (say, an image of a car or a person) on a map and show a toast message upon arriving at the destination location. Develop incrementally P1: Place a marker at a certain location P2: Trace current location by moving the marker P3: Check if the marker is near the destination Q: How to detect if one is within a certain distance (in miles)? A: Use Location.distanceBetween method, e.g., float result = 0; final float[] dist = new float[1]; Location.distanceBetween(lat1, lng1, lat2, lng2, dist); result = dist[0] * 0.000621371192f; // coz dist[0] is in meters 20


Download ppt "Location-Based Services. Objectives How to display Google Maps in your application How to control displayed maps How to perform geocoding and reverse."

Similar presentations


Ads by Google