Download presentation
Presentation is loading. Please wait.
1
Location-Based Services: Part 2 (Google Maps)
2
Accessing Google Maps Google maps can be accessed in two ways
Through a browser or a WebView Through the Google Maps Android API v2 Google Maps Android API v2 allows you to incorporate Google Maps into applications is distributed as part of the Google Play Services SDK encapsulates maps in a MapFragment or a SupportMapFragment MapFragment and SupportMapFragment essentially replace the MapActivity class used in version 1. ©SoftMoore Consulting
3
Google Maps Android API v2
Using Google Maps Android API v2, you can Add maps to your app 3D maps − terrain maps satellite maps − etc. Customize the map markers − image overlays polylines/polygons − etc. Control the user’s view zoom − pan rotate − etc. ©SoftMoore Consulting
4
Attribution Requirements
If you use the Google Maps Android API in your application, you must include the Google Play Services attribution text as part of a “Legal Notices” section in your application. Including legal notices as an independent menu item, or as part of an “About” menu item, is recommended. The attribution text is available by making a call to method GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo() ©SoftMoore Consulting
5
Getting Started Download and configure the Google Play services SDK, which includes the Google Maps Android API. Obtain a Google Maps API key. register a project in the Google APIs Console get a Google Maps key associated with the signing certificate and package for your app Add the required settings in your application’s manifest. Add a map to your application. Test your application Android Studio helps simplify the middle three steps. ©SoftMoore Consulting
6
Download Google Play Services (Android SDK Manager)
©SoftMoore Consulting
7
Creating a New Application with a Google Maps Activity
Make sure that the Google Play services SDK is installed, as shown in previous slide. Using Android Studio, start with a Google Maps Activity when creating a new project or add a Google Maps Activity to an existing project. The resource google_maps_api.xml contains the information and steps needed to obtain a Google Maps API key. The slides at the end of this section provide the details needed to add Google Maps to an application if you do not create a Google Maps Activity in Android Studio. ©SoftMoore Consulting
8
Test the Application Note: By default the Google Maps
Activity does not contain an action bar. ©SoftMoore Consulting
9
Map UI Properties Similar to other Android UI properties, most map UI properties can be set using one of two approaches declarative (in an XML file) procedural approach (in the Java code). As usual, the declarative approach is generally preferred. ©SoftMoore Consulting
10
Classes MapFragment and SupportMapFragment
Classes MapFragment and SupportMapFragment (in package com.google.android.gms.maps) are the simplest ways to place a map in an application. Use SupportMapFragment if you need to support Android devices running API 10 and lower. These classes wrap a view of a map that automatically handles the necessary life cycle needs. Add a MapFragment or a SupportMapFragment to an activity’s layout file. ©SoftMoore Consulting
11
Selected XML Attributes for a MapFragment
mapType values include none, normal, hybrid, satellite, and terrain uiZoomControls, uiCompass specify whether you want the zoom controls and compass to appear on the map uiZoomGestures, uiScrollGestures, etc. specify which gestures are enabled/disabled liteMode map that supports a subset of the map functionality Add these attributes using the following namespace declaration: xmlns:map=" ©SoftMoore Consulting
12
SupportMapFragment Example
<fragment xmlns:android=" xmlns:tools=" xmlns:map=" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MapsActivity" android:name="com.google.android.gms.maps.SupportMapFragment" map:uiZoomControls="true" /> Note: This map fragment includes zoom controls. ©SoftMoore Consulting
13
Class GoogleMap GoogleMap models the map object within an application.
GoogleMap automatically handles Connecting to the Google Maps service. Downloading map tiles. Displaying tiles on the device screen. Displaying various controls such as pan and zoom. Responding to pan and zoom gestures by moving the map and zooming in or out. ©SoftMoore Consulting
14
The OnMapReadyCallback Interface
The OnMapReadyCallBack interface contains a single abstract method. public void onMapReady(GoogleMap map) Implement the OnMapReadyCallback interface to get access to a GoogleMap object. Use the GoogleMap object to set map properties. ©SoftMoore Consulting
15
Implementing the OnMapReadyCallback Interface
Import the necessary map classes and interfaces. import com.google.android.gms.maps.*; import com.google.android.gms.maps.model.*; Declare that the activity implement the interface. public class MainActivity extends ActionBarActivity implements OnMapReadyCallback ©SoftMoore Consulting
16
Implementing the OnMapReadyCallback Interface (continued)
Use getMapAsync() to set the callback on the map fragment. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(this); } Or use class SupportMapFragment and method getSupportFragmentManager() ©SoftMoore Consulting
17
Implementing the OnMapReadyCallback Interface (continued)
Implement the onMapReady() method. @Override public void onMapReady(GoogleMap map) { LatLng charleston = new LatLng(32.781, ); map.setMyLocationEnabled(true); map.moveCamera(CameraUpdateFactory .newLatLngZoom(charleston, 14)); map.addMarker(new MarkerOptions() .title("Charleston, S.C.") .snippet("The most beautiful city in North America.") .position(charleston)); } ©SoftMoore Consulting
18
Google Map Example (continued)
©SoftMoore Consulting
19
Creating a Virtual Device for Google Maps
Make sure that you have installed (using the Android SDK manager) a Google APIs system image for the virtual device target. Make sure that the virtual device target supports Google APIs; i.e., the phrase “Google APIs” should appear somewhere in the target name. ©SoftMoore Consulting
20
Creating a Virtual Device for Google Maps (continued)
©SoftMoore Consulting
21
Creating a Virtual Device for Google Maps (continued)
©SoftMoore Consulting
22
Adding Google Maps to an Application
The remaining slides in this section provide the details needed to add Google Maps to an application if you do not create a Google Maps Activity in Android Studio. ©SoftMoore Consulting
23
Setting Up Google Play Services (https://developer. android
Make sure that the Google Play services SDK is installed, as shown in previous slide. In Android Studio under “Gradle Scripts”, edit the build.gradle file for “Module: app” (not the build.gradle file for the project) Under dependencies (near the bottom), add the following line at the end: compile 'com.google.android.gms:play-services-maps:6.5.+' Also add this line if you are combining maps with other location services such as location updates. compile 'com.google.android.gms:play-services-location:6.5.+' ©SoftMoore Consulting
24
Setting Up Google Play Services (continued)
Save the changes and click “Sync Project with Gradle Files” in the toolbar, or click on menu item Tools Android Sync Project with Gradle Files. Edit file AndroidManifest.xml and add the following tag as a child of the <application> element: <meta-data android:name="com.google.android.gms.version" Note: You can ignore instructions about creating a ProGuard exception if you are building in debug mode (i.e., not release mode). ©SoftMoore Consulting
25
Obtaining a Maps API Key
Before using the Google Maps service, you need to obtain a Google Maps API key. If you created a Google Maps Activity using Android Studio, then the resource google_maps_api.xml contains the information and steps needed to obtain a Google Maps API key. Otherwise you must Use your certificate’s SHA1 fingerprint to register with the Maps service to obtain a Maps API key Include the Maps API key in MapView elements of the XML layout files Sign the application with the same certificate used to obtain the Maps API key ©SoftMoore Consulting
26
The SDK Debug Key Recall that during installation, the Android SDK tools create a debug keystore and key in <ANDROID_SDK_HOME>\.android with predetermined names/passwords: Keystore name: “debug.keystore” Keystore password: “android” Key alias: “androiddebugkey” Key password: “android” At each compilation, the SDK tools use this debug key to sign the .apk file. ©SoftMoore Consulting
27
Obtaining the Debug Key’s SHA1 Fingerprint
Use Java’s keytool “- list” option to obtain the certificate’s SHA1 fingerprint. Example C:\Java\Android\sdk\android-sdk\.android>keytool –list -keystore debug.keystore Enter keystore password: Keystore type: JKS Keystore provider: SUN Your keystore contains 1 entry androiddebugkey, Dec 14, 2014, PrivateKeyEntry, Certificate fingerprint (SHA1): 37:97:13:32:E7:57:08:56:9A:E7:51:CB:43:B3:... all on one line (enter “android”) response ©SoftMoore Consulting
28
Register with the Google Maps Service to Obtain a Maps API Key
To get a Google Maps API key, go to Scroll to “Create an API project in the Google APIs Console” and follow instructions to register your project for the Maps API. Then scroll to “Obtain a Google Maps API key” and follow the instructions to obtain an API key. You will need to enter the certificate’s fingerprint and one or more package names; e.g., 37:97:13:32:E7:57:08:56:9A:E7:...;edu.citadel.android.maps ©SoftMoore Consulting
29
Register with the Google Maps Service to Obtain a Maps API Key (continued)
When submitted, you will get back a page with an API key that can be used for apps signed with your certificate: following information; e.g., something like AIzaSyBI6yPiqwvpUH8_X7w_HNE2yiHhpDuUD58 ©SoftMoore Consulting
30
Configure the Application’s Manifest
Add the following element as a child of the <application> element: <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyBI6yPiqwvpUH8_X7w_HNE2yiHhpDuUD58"/> Specify permissions; e.g., android.permission.INTERNET android.permission.ACCESS_NETWORK_STATE android.permission.WRITE_EXTERNAL_STORAGE android.permission.ACCESS_FINE_LOCATION Use your Maps API key here. (or COARSE_LOCATION) ©SoftMoore Consulting
31
Configure the Application’s Manifest (continued)
Recommended: To prevent Google Play Store from displaying the app on devices that don't support OpenGL ES version 2, add the following element as a child of the <manifest> element: <uses-feature android:glEsVersion="0x " android:required="true"/> ©SoftMoore Consulting
32
Create Simple Test App Create a basic new project in Android Studio, and replace the layout file (activity-main.xml) with the following: <?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android=" xmlns:map=" android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.google.android.gms.maps.MapFragment" /> ©SoftMoore Consulting
33
Test the Simple App ©SoftMoore Consulting
34
Relevant Links Google Maps Android API v2
Setting Up Google Play Services Map Objects Signing Your Applications Google Maps Sample Project <android-sdk>/extras/google/google_play_services/samples/maps ©SoftMoore Consulting
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.