csf645-maps

advertisement
CSF645 – Mobile Computing
行動計算
Google Maps Android API v2
https://developers.google.com/maps/documentation/android/
吳俊興
國立高雄大學 資訊工程學系
Introduction to the Google Maps Android API v2
• The API automatically handles
– access to Google Maps servers
– data downloading
– map display, and
– response to map gestures
• Also use API calls
– To add markers, polygons, and overlays to a basic map, and
– To change the user's view of a particular map area
2
Google Maps Android API v2 Improvements
• Distributed as part of the Google Play services SDK
• Encapsulated in the MapFragment class, an extension of
Android's Fragment class, rather in the Activity class
– Able to add a map as a piece of a larger Activity
– Able to implement them by extending the Android standard Activity class,
rather than extending the MapActivity used in version 1
• Using vector tiles: data representation is smaller, with less
bandwidth
• Caching is improved, so users will typically see a map without
empty areas.
• Supporting 3D maps
3
Overall Process of Adding a Map to an App
• Step 1. Download and configure the Google Play services SDK
– The Google Maps Android API is distributed as part of this SDK
• Step 2. Obtain an API key
– To do this, you will need to register a project in the Google APIs Console,
and get a signing certificate for your app
• Step 3. Specify settings in the Application Manifest
• Step 4. Add a map to a new or existing Android project
• Run the Sample Code - com.example.mapdemo
https://developers.google.com/maps/documentation/android/start
4
Run the Sample Code - com.example.mapdemo
• Make sure Google Play services added as an Android library
project
1. Select File > Import > Android > Existing Android Code Into Workspace and click
Next
2. Select Browse..., enter <share>/google-play-services_lib, and click Finish
• View the sample code, or run the sample app locally
– Sample code is bundled with the Google Play services SDK
1. Select File > Import > Android > Existing Android Code Into Workspace and click
Next.
2. Select Browse..., enter <android-sdkfolder>/extras/google/google_play_services/samples/maps, and click Finish
3. Select Project > Properties, select Java Build Path, and navigate to Libraries.
4. Select Add External Jars, include the following jar files, and click OK: + <androidsdk-folder>/extras/android/support/v4/android-support-v4.jar
5. Add your own Google Maps Android API key
6. Select Run > Run to test the sample app
5
Step 1. Setup Google Play Services SDK
• To develop an app using the Google Play services APIs,
download the Google Play services SDK from the SDK
Manager
– The download includes the client library and code samples
• To test your app when using the Google Play services SDK,
you must use either:
– A compatible Android device that runs Android 2.2 or higher and includes
Google Play Store
– The Android emulator with an AVD that runs the Google APIs platform
based on Android 4.2.2 or higher
• Three steps:
– S1-1. Install the Google Play Services SDK
– S1-2. Set Up a Project with the Library
– S1-3. Ensure Devices Have the Google Play services APK
http://developer.android.com/google/play-services/setup.html
6
S1-1. Install the Google Play Services SDK
1. Launch the SDK Manager
– From Eclipse (with ADT), select Window > Android SDK Manager
2. Install the Google Play services SDK
– Saved in <android-sdk>/extras/google/google_play_services/
3. Install a compatible version of the Google APIs platform
– To test app on the emulator, expand the directory for Android 4.2.2 (API 17) or a higher version, select
Google APIs, and install it
4. Make a copy of the Google Play services library project
– Copy the library project at <android-sdk>/extras/google/google_play_services/libproject/google-playservices_lib/ to the location where you maintain your Android app projects (i.e. adt-bundle/share)
– import the library project into your workspace. Click File > Import, select Android > Existing Android Code
7
into Workspace, and browse to the copy of the library project to import it
S1-2. Set Up a Project to Use
Google Play Services SDK
• Reference the library project in your Android project
1. Make sure that both the project library and the application project that depends
on it are in your workspace. If one of the projects is missing, import it into your
workspace
2. In the Package Explorer, right-click the dependent project and select
Properties
3. In the Properties window, select the "Android" properties group at left and locate
the Library properties at right
4. Click Add to open the Project Selection dialog
5. From the list of available library projects, select a project and click OK
6. When the dialog closes, click Apply in the Properties window
7. Click OK to close the Properties window
8
Step 2. Obtain an API Key
• The Google Maps Android API v2 uses a new system of
managing keys
– Existing keys from a Google Maps Android v1 application, commonly
known as MapView, will not work with the v2 API
– Obtain a Maps API key from the Google APIs Console by providing the
application's signing certificate and its package name
– Once having the key, add it to the application by adding an element to the
application's manifest file AndroidManifest.xml
• Maps API keys are linked to specific certificate/package pairs
– Sign each of your applications with a different certificate and get a
different key for each one
• Four steps:
– S2-1: Retrieve information about your application's certificate
– S2-2: Register a project in the Google APIs Console and add the Maps
API as a service for the project
– S2-3: Once you have a project set up, you can request one or more keys
Add your key to your application and begin development
9
S2-1. Displaying Certificate Information
• An app has two certificates
– Debug certificate: only for use with an application for testing
– Release certificate
• SHA-1 fingerprint: a short form of application's digital certificate
– Eclipse goes to Windows > Preferences > Android > Build
10
S2-2. Obtaining an API Key
• Go to https://code.google.com/apis/console/
1. API Project > Services: Enable Google Maps Android API v2
2. API Project > API Access > Create New Android Key....
3. Enter the SHA-1 fingerprint, then a semicolon, then your application's
package name
4. The Google APIs Console responds by displaying Key for Android apps
(with certificates) followed by a forty-character API key
5. Copy this key value
AIzaSyDZVhi1AFAqVN6djSpXLq36dvljPSeVFGU
11
S2-3. Adding the API Key to the Application
1.In AndroidManifest.xml, add the following element as a child of the <application> element,
by inserting it just before the closing tag </application>:
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="your_api_key"/>
substituting your API key for your_api_key. This element sets the
key com.google.android.maps.v2.API_KEY to the valueyour_api_key and makes the API key
visible to any MapFragment in your application.
2.Add the following elements to your manifest. Replace com.example.mapdemo with the
package name of your application.
<permission
android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE"/>
3.Save AndroidManifest.xml and re-build your application.
12
Download