按照以下步骤操作:
Create a new project
File -> New -> Android Application Project
You can leave all the options with their default values
Choose BlankActivity in the Create Activity screen
Ensure you have the latest version (v2) of the Google Play Services installed.
Select Window > Android SDK Manager or run android at the command line.
Scroll to the bottom of the package list and select Extras > Google Play services. The Google Play services SDK is downloaded to your computer and installed in your Android SDK environment at <android-sdk-folder>/extras/google/google_play_services/
Add the Google Play Services project into your Eclipse workspace.
Click File -> Import..., select Android -> Existing Android Code into Workspace
Browse to and select <android-sdk-folder>/extras/google/google_play_services/libproject/google-play-services_lib
To add the dependency to Google Play Services into your project
Project -> Properties -> Android -> Library, Add -> google-play-services_lib
Update MainActivity.java to extend FragmentActivity instead of Activity
Check that Google Play Services is available on the device at runtime to protect against error cases
GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
Update res/layout/activity_main.xml and replace the entire contents with
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
The map is controlled through the GoogleMap object, use findFragmentById to keep a reference to the GoogleMap from the layout.
GoogleMap 地图 = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
要使用 Google Maps API,您需要一个 API 密钥:
Go to the Google APIs Console
In Services enable ‘Google Maps Android API v2’ and agree to the terms
In the left navigation bar, click API Access.
Click Create New Android Key....
In the resulting dialog, enter the SHA-1 fingerprint, then a semicolon, then your application's package name. For example:
BB:0D:AC:74:D3:21:E1:43:67:71:9B:62:91:AF:A1:66:6E:44:5D:75;com.example.android.mapexample
If you have debug and production keys then add both in.
To obtain the SHA1 of your debug certificate use keytool:
keytool -list -alias androiddebugkey -keystore ~/.android/debug.keystore -storepass android -keypass android -v
For Windows computers the .android directory is in your user directory
Your new API key will be a 40 character string like AIzbSyBaEqeUpYvlsbUe0OqTPn1-ABVMHVbdNxo
Add the following tag into your AndroidManifest.xml just before the closing </application> tag
android:name="com.google.android.maps.v2.API_KEY"
android:value="your_api_key"/>
Your application now also needs the following permissions in the AndroidManifest.xml
android:name="your_package_name.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
Optionally you can include one or both of these permissions to enable auto-location
Maps v2 uses OpenGl so the following uses-feature is also required
Test your application, checking the logcat output for any errors.
If it is working try out some of the other GoogleMap options:
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
mMap.setMyLocationEnabled(true);
more details at
https://developers.google.com/maps/documentation/android/views
归属要求。如果您在应用程序中使用 Google Maps Android API,则必须将 Google Play 服务归属文本作为“法律声明”部分的一部分包含在您的应用程序中。建议将法律声明作为一个独立的菜单项,或作为“关于”菜单项的一部分。可通过调用 GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo 获得归属文本。
来源:HERE