Google Maps Android API

本文档详述了如何从零开始使用 Google Maps Android API V2,包括申请密钥、配置 AndroidManifest.xml 文件、集成支持库以及在 Activity 中加载地图等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

google map android api 2 比api 1 改变很多,虽然老版本可以继续使用,但是想开发新的项目必须申请新的key所以就要熟悉google map api 2.

参考开发者 https://developer.android.com/google/play-services/maps.html

需要的的准备工程有1.google-play-service-lib 2.两个apk(如果手机有装最新的  play 商店 ,google 设置可以不用,一定要最新的,不然打开地图会提示你更新)

lib的下载地址http://download.youkuaiyun.com/detail/chaoyue0071/8268901

apk的下载地址http://download.youkuaiyun.com/detail/chaoyue0071/8268929

一,申请key。国内记得翻墙
获取Maps API key需要两样东西:应用的signing certificate和它的package name。
获取这个key之后,把它加在应用程序的AndroidManifest.xml文件里即可。
为应用获取一个key还是需要好几个步骤的,下面详细说明:
获取数字证书(digital certificate)信息
数字证书有Debug和Release两种,下面主要说Debug的。
要获取一个叫做SHA-1 fingerprint的东西,作为数字证书的一个简短代表。
这个指纹(fingerprint)是通过一个哈希算法得到的字符串,为了得到你的证书的SHA-1 fingerprint,首先要找到你的debug keystore 文件,文件名叫debug.keystore。
  默认情况下它和虚拟机AVD存放在一起,win7下的路径是:C:\Users\your_user_name\.android\,也可以通过Eclipse中的Windows > Prefs > Android > Build来查看这个路径。
然后,在cmd命令行里运行下列命令:
keytool -list -v -keystore "C:\Users\your_user_name\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android
就显示一大堆东西,其中就有证书指纹:

SHA1那一行就包含了证书的SHA-1 fingerprint,是二十段用冒号割开的数字段,每段是两个十六进制的数。在Google APIs Console上创建API Project

在Google APIs Console上创建项目,并且注册Maps API。

首先,去这个网址:https://code.google.com/apis/console/

用Gmail的账户登录,如果是第一次的话,需要创建项目,默认情况会创建一个叫做API Project的项目。
点击左边的Services,会在中间看到很多的APIs和Services,找到Google Maps Android API v2,然后把它设置成on,需要接受一些服务条款。
获得API Key
在左边的导航条中选择API Access。
在出来的页面中选择Create New Android Key...就可以生成key了:

然后在对话框中填入:SHA-1 指纹, 分号隔开,然后是应用的 package name.然后就会生成一个Key。
比如:  

2.androidmainfest 添加配置信息

<permission
        android:name="com.example.googlemapdemo.permission.MAPS_RECEIVE"
       android:protectionLevel="signature"/>
    <uses-permissionandroid:name="com.example.googlemapdemo.permission.MAPS_RECEIVE"/>
    <uses-permissionandroid:name="android.permission.INTERNET"/>
    <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permissionandroid:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>

其中com.example.mapdemo换成自己的包名。在appliation中也要添加meta-data

<meta-data
            android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version"/>
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyBk8VkFPfUamrOMBo5ToNFhmhuKayMws4Q"/>


其中第二个value就要换成你申请到的key

3.布局文件添加

<fragmentxmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>


supportmapfragment是可以支持11版本一下的

4.activity代码

package com.example.googlemapdemo;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends FragmentActivity {

	private GoogleMap mMap;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		 setUpMapIfNeeded();
	}
	
	@Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

	private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
            
        }
    }

    /**
     * This is where we can add markers or lines, add listeners or move the camera. In this case, we
     * just add a marker near Africa.
     * <p>
     * This should only be called once and when we are sure that {@link #mMap} is not null.
     */
    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值