Chapter 11: Discover! Incorporating Google Maps
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
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
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
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
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
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
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
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
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
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
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
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
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 http://developers.google.com/android/maps-api-signup Android Boot Camp for Developers using Java
Obtaining a Maps API Key from Google (continued) Figure 11-10 Android Maps API Key Signup Web Site Android Boot Camp for Developers using Java
Obtaining a Maps API Key from Google (continued) Figure 11-11 Unique MD5 fingerprint code Android Boot Camp for Developers using Java
Obtaining a Maps API Key from Google (continued) Figure 11-12 Android Maps API key Android Boot Camp for Developers using Java
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
Obtaining a Maps API Key from Google (continued) <com.google.android.maps.MapView android:id="@ +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
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
Obtaining a Maps API Key from Google (continued) Figure 11-16 Android Manifest code with the Google library permission Android Boot Camp for Developers using Java
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
Understanding MapView (continued) Figure 11-19 Main Extends MapActivity Figure 11-20 Instance of MapView and the zoom controls set to true Android Boot Camp for Developers using Java
Understanding MapView (continued) Figure 11-21 Google Maps displayed in the Android emulator Android Boot Camp for Developers using Java
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
Adding Overlay Items (continued) Figure 11-22 Overlay.java class Android Boot Camp for Developers using Java
Adding Overlay Items (continued) Figure 11-23 Overlay.java class automated code Android Boot Camp for Developers using Java
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
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
Adding Overlay Items (continued) Figure 11-25 Overlay constructor Android Boot Camp for Developers using Java
Adding Overlay Items (continued) Figure 11-26 The addOverlay method populates the pushpin images Android Boot Camp for Developers using Java
Adding Overlay Items (continued) Figure 11-27 OverlayItem method Android Boot Camp for Developers using Java
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
Adding Overlay Items (continued) Figure 11-29 onTap() method Android Boot Camp for Developers using Java
Adding Overlay Items (continued) Figure 11-30 Instance named item and the AlertDialog box are coded Android Boot Camp for Developers using Java
Adding Overlay Items (continued) Figure 11-31 Complete code of Overlay.java Android Boot Camp for Developers using Java
Adding Overlay Items (continued) Coding the Drawable Overlay Figure 11-32 List of Overlay items Figure 11-33 The pushpin image becomes the map marker Android Boot Camp for Developers using Java
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 37.4198°, -79.14°, which yields coordinates of 37419800, -79140000 microdegrees Android Boot Camp for Developers using Java
Locating a GeoPoint (continued) Coding the GeoPoint Location Figure 11-35 First GeoPoint Android Boot Camp for Developers using Java
Locating a GeoPoint (continued) Figure 11-36 Second GeoPoint Android Boot Camp for Developers using Java
Locating a GeoPoint (continued) Figure 11-37 Main.java complete code Android Boot Camp for Developers using Java
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
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
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
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
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