Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 11: Discover! Incorporating Google Maps

Similar presentations


Presentation on theme: "Chapter 11: Discover! Incorporating Google Maps"— Presentation transcript:

1 Chapter 11: Discover! Incorporating Google Maps

2 Objectives In this chapter, you learn to:
Create an Android project displaying a Google map Install the Google API to the SDK Set up a Google API Android Virtual Device Locate your MD5 certificate Sign up for a Google Maps API key Understand security and permissions Android Boot Camp for Developers using Java

3 Objectives (continued)
Access the MapView class Code the populate( ) method Add the onTap( ) method Set permissions for maps in the Android Manifest file Create a GeoPoint overlay Android Boot Camp for Developers using Java

4 Using Google Maps Smartphones can access online maps using a built-in Android Google Maps application Users can zoom in to see details Can be customized Figure 11-1 Maps Application Android Boot Camp for Developers using Java

5 Using Google Maps (continued)
Steps to complete the App: Install the Google API add-on to the SDK. Add the AVD that uses the Google API deployment target. Obtain a personal Maps API key from Google. Define a MapView inside a Linear layout in main.xml. Add permissions to the Android Manifest file to access the Internet and the Google library. Add a no title bar theme to the Android Manifest file. Add the pushpin image to the drawable folder. Code the MapView in Main.java. Add Overlay objects to the map. Call the populate( ) method to read each Overlay object. Display two GeoPoint overlays. Android Boot Camp for Developers using Java

6 Using Google Maps (continued)
Google Maps is a Free online mapping service offering things like: Turn-by-turn directions GPS Location Services Directions to a hotel Distance of a morning run Street view images Bike path directions Possible Traffic delays Public transit routes Began in the United States in 2005 Updated Frequently Android Boot Camp for Developers using Java

7 Installing the Google API
API – Application Programming Interface A set of tools for building software applications Downloading, rendering, and caching of map files Must be downloaded and installed in your SDK environment Figure 11-3 Android SDK Manager Android Boot Camp for Developers using Java

8 Installing the Google API (continued)
Adding the AVD to Target the Google API Android Virtual Device (AVD) must be set to use the Google API Figure 11-4 Android Virtual device Manager dialog box Android Boot Camp for Developers using Java

9 Installing the Google API (continued)
Figure 11-6 Google API displayed in the AVD list Figure 11-5 Create new Android Virtual Device (AVD) dialog box Android Boot Camp for Developers using Java

10 Obtaining a Maps API Key from Google
Must apply for a free Google Maps API key Register and agree to terms with Google Maps Service Your computer’s MD5 (Message-Digest Algorithm 5) digital fingerprint verifies the integrity of the file A unique Google Maps API key is generated: Certificate fingerprint (MD5): 94:1E:43:49:87:73:BB:E6:A6:88:D7:20:F1:8E:B5:98 Android Boot Camp for Developers using Java

11 Obtaining a Maps API Key from Google (continued)
You cannot run a Google Map app if it is not signed with your local API key. Key is stored in a file named debug.keystore Figure 11-7 Location of the debug.keystore file on a Windows 7 computer Android Boot Camp for Developers using Java

12 Obtaining a Maps API Key from Google (continued)
Figure 11-8 MD5 fingerprint in the Command Prompt window Android Boot Camp for Developers using Java

13 Obtaining a Maps API Key from Google (continued)
Troubleshooting: Keytool not recognized You need to locate the keytool executable file on your computer A fingerprint other than MD5 is generated A fingerprint such as SHA1 might appear instead of the MD5 fingerprint Android Boot Camp for Developers using Java

14 Obtaining a Maps API Key from Google (continued)
Registering the MD5 Fingerprint with the Google Maps Service A single Maps API key is valid for all applications signed by a single certificate Place theMD5 fingerprint in the Google Registration page You need a Gmail account to receive the API key Place the API key in the XML layout code Android Boot Camp for Developers using Java

15 Obtaining a Maps API Key from Google (continued)
Figure Android Maps API Key Signup Web Site Android Boot Camp for Developers using Java

16 Obtaining a Maps API Key from Google (continued)
Figure Unique MD5 fingerprint code Android Boot Camp for Developers using Java

17 Obtaining a Maps API Key from Google (continued)
Figure Android Maps API key Android Boot Camp for Developers using Java

18 Obtaining a Maps API Key from Google (continued)
Adding the MapView element in the XML Code API key attribute holds the Google Maps API key that proves your application and signed certificate are registered with the Google Maps service the API key must be added to the main.xml layout file You must use the <com.google.android.maps.MapView/> element to display the Google Maps in your Activity Android Boot Camp for Developers using Java

19 Obtaining a Maps API Key from Google (continued)
<com.google.android.maps.MapView +id/mapview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:enabled="true" android:clickable="true" android:apiKey="0HljqLj_jO8oBj4g8zSxyEuezie5-mE_56_UiXA“ /> Note: Your generated apiKey will be different. Android Boot Camp for Developers using Java

20 Obtaining a Maps API Key from Google (continued)
Adding Permissions to the Android Manifest File Permissions are necessary to prevent malicious outside applications from corrupting data and accessing sensitive information including: Full access to the Internet Your GPS location Your personal information Phone calls SMS Messages Other system tools Android Boot Camp for Developers using Java

21 Obtaining a Maps API Key from Google (continued)
Figure Android Manifest code with the Google library permission Android Boot Camp for Developers using Java

22 Understanding MapView
The MapView class displays and manipulates a Google Map The setBuiltInZoomControls property allows the site visitor to use the built-in zoom feature MapView mapView = (MapView) findViewById(R.id.mapview); mapView.setBuiltInZoomControls(true); Android Boot Camp for Developers using Java

23 Understanding MapView (continued)
Figure Main Extends MapActivity Figure Instance of MapView and the zoom controls set to true Android Boot Camp for Developers using Java

24 Understanding MapView (continued)
Figure Google Maps displayed in the Android emulator Android Boot Camp for Developers using Java

25 Adding Overlay Items Overlays – also called Map markers – use a graphic image to indicate a specific location on a map Use the ItemizedOverlay class to manage the individual items placed as a layer on the map Android Boot Camp for Developers using Java

26 Adding Overlay Items (continued)
Figure Overlay.java class Android Boot Camp for Developers using Java

27 Adding Overlay Items (continued)
Figure Overlay.java class automated code Android Boot Camp for Developers using Java

28 Adding Overlay Items (continued)
Adding Overlay Objects to an ArrayList Assign an expandable array called ArrayList Customized constructors needed to define default markers Populate() method used to add each pushpin item to the display Android Boot Camp for Developers using Java

29 Adding Overlay Items (continued)
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); private Context mContext; public Overlay(Drawable defaultMarker, Context context) { super(boundCenterBottom(defaultMarker)); mContext = context; // TODO Auto-generated constructor stub } public void addOverlay(OverlayItem overlay) { mOverlays.add(overlay); populate(); @Override protected OverlayItem createItem(int i) { // TODO Auto-generated method stub return mOverlays.get(i); public int size() { return mOverlays.size(); Android Boot Camp for Developers using Java

30 Adding Overlay Items (continued)
Figure Overlay constructor Android Boot Camp for Developers using Java

31 Adding Overlay Items (continued)
Figure The addOverlay method populates the pushpin images Android Boot Camp for Developers using Java

32 Adding Overlay Items (continued)
Figure OverlayItem method Android Boot Camp for Developers using Java

33 Adding Overlay Items (continued)
Coding the onTap Method onTap() method receives the index of the overlay item selected by the user The AlertDialog box displays a message to the user The show() method is actually used to display the dialog box @Override protected boolean onTap(int index) { OverlayItem item = mOverlays.get(index); AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); dialog.setTitle(item.getTitle()); dialog.setMessage(item.getSnippet()); dialog.show(); return true; } Android Boot Camp for Developers using Java

34 Adding Overlay Items (continued)
Figure onTap() method Android Boot Camp for Developers using Java

35 Adding Overlay Items (continued)
Figure Instance named item and the AlertDialog box are coded Android Boot Camp for Developers using Java

36 Adding Overlay Items (continued)
Figure Complete code of Overlay.java Android Boot Camp for Developers using Java

37 Adding Overlay Items (continued)
Coding the Drawable Overlay Figure List of Overlay items Figure The pushpin image becomes the map marker Android Boot Camp for Developers using Java

38 Locating a GeoPoint Default Google map is of the United States
Any worldwide location can be found by panning and zooming Map location – called the GeoPoint – contains latitude and longitude coordinates GeoPoint locations are in microdegrees (degrees * 1e6) For example, Lynchburg Virginia’s latitude and longitude coordinates are °, °, which yields coordinates of , microdegrees Android Boot Camp for Developers using Java

39 Locating a GeoPoint (continued)
Coding the GeoPoint Location Figure First GeoPoint Android Boot Camp for Developers using Java

40 Locating a GeoPoint (continued)
Figure Second GeoPoint Android Boot Camp for Developers using Java

41 Locating a GeoPoint (continued)
Figure Main.java complete code Android Boot Camp for Developers using Java

42 Summary To use Google Maps, you must install the Google API (application programming interface) in the Android SDK and then embed the Google Maps site directly into an Android application and overlay app-specific data on the map Set the application’s properties to select the Google APIs add-on as the build target, which sets the Android Virtual Device (AVD) Manager to use the new Google API You need to apply for a free Google Maps API key to integrate Google Maps into your Android application Android Boot Camp for Developers using Java

43 Summary (continued) Using the MD5 fingerprint, you can register with Google for aMaps API key at the onlineGoogleMaps service. To display a Googlemap in an Android app, you must add the API key to the main.xml layout file in your project You must use the <com.google.android.maps.MapView> element to display the Google Maps in your Activity Android Boot Camp for Developers using Java

44 Summary (continued) Android apps use permissions to prevent malicious outside applications from corrupting data and accessing sensitive information. Apps need permission to access the Internet and connect with the Google mapping feature Google mapping technology relies on the Android MapView class Android Boot Camp for Developers using Java

45 Summary (continued) An instance of MapView uses the setBuiltInZoomControls property. When set to true, this property allows site visitors to use the built-in zoom feature on the map in your Android app A map marker, or overlay, uses a graphic image such as a pushpin to indicate a specific location on a map The Overlay class assigns an ArrayList to hold the overlay objects, such as pushpin images, displayed in a layer on the map Android Boot Camp for Developers using Java

46 Summary (continued) Use the populate( ) method to add each new item in the ItemizedOverlay to the map Use the onTap() method to display a text message Use an instance of the MapView class to list points of interest Users can pan and zoom to find any location in the world from the default USA map GeoPoints contain latitude & longitude coordinates Use the add( ) method to display the GeoPoints as an overlay on the map Android Boot Camp for Developers using Java


Download ppt "Chapter 11: Discover! Incorporating Google Maps"

Similar presentations


Ads by Google