【如何在Android中使用GoogleMap API】

本文详细介绍了如何在Android应用中集成Google Maps API,包括下载Google APIs、获取Map API Key及在AndroidManifest.xml中添加必要的权限等步骤。

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

    各位早上好 ^ ^,bill好久没有写博文了,趁着新任务来临之前,抓紧时间写点东西。

    这段时间在做一个android应用,应用中需要用到google map api,原以为直接在Android SDK Manager中下载Google APIs就能使用了,没想到还要做一些工序,bill通过网络学习到了相关知识,谨作为备忘记载于此。废话不多说,奉上步骤。

    前面说到,需要利用Android SDK Manager下载Google APIs,以android 2.1 updates为例

    点击勾选“Google APIs”进行下载安装,安装完成后,即可建立以Android 2.1updates版本下的附带Google APIs的Android应用

    到此为止,开发一个附带Google Map的应用的“硬件”设施已经准备就绪。接下来我们需要备齐各种“软件”。

    首先,我们需要Google Map API Key,至于为什么,我还是直接拷贝Google的原话吧。

[quote]Because MapView gives you access to Google Maps data, you need to register with the Google Maps service and agree to the applicable Terms of Service before your MapView will be able to obtain data from Google Maps. This will apply whether you are developing your application on the emulator or preparing your application for deployment to mobile devices.

    我们需要先下载一个小软件——key tool,以eclipse IDE为例,点击“帮助”→“安装新软件”


    点击右侧的“添加”,填写KeyTool的挂载站点如下

http://www.keytool.sourceforge.net/update

    下载安装完成之后,重启eclipse,在工具栏上能够看到key tool,点击并选择open keystore

    在弹出窗口中打开你文件目录下的/.../.../.android/debug.keystore文件,密码默认为android。接着打开Certificate,将里面的MD5码拷贝并填写到google的注册网址,获得Map API Key。将得到的Map Key保存好以备后用。

    接下来,我们就可以在自己的应用当中使用Google Map了。

    首先,我们需要在AndroidManifest配置文件中添加Google Map相关的权限和类库

 

 
 
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     package="com.billhoo.study.hellogooglemaps" 
  3.     android:versionCode="1" 
  4.     android:versionName="1.0" > 
  5.  
  6.     <uses-sdk 
  7.         android:minSdkVersion="7" 
  8.         android:targetSdkVersion="15" /> 
  9.   
  10.     <!-- access to fine location --> 
  11.     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
  12.  
  13.     <!-- access to coarse location --> 
  14.     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
  15.  
  16.     <application 
  17.         android:icon="@drawable/ic_launcher" 
  18.         android:label="@string/app_name" 
  19.         android:theme="@style/AppTheme" > 
  20.         <activity 
  21.             android:name=".MainActivity" 
  22.             android:theme="@android:style/Theme.NoTitleBar" > 
  23.             <intent-filter> 
  24.                 <action android:name="android.intent.action.MAIN" /> 
  25.  
  26.                 <category android:name="android.intent.category.LAUNCHER" /> 
  27.             </intent-filter> 
  28.         </activity> 
  29.  
  30.         <!-- google map library --> 
  31.         <uses-library android:name="com.google.android.maps" /> 
  32.     </application> 
  33.  
  34. </manifest> 

    然后,我们建立一个使用Google Map的布局并在Activity上显示。

 
 
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="http://schemas.android.com/tools" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" > 
  5.  
  6.     <com.google.android.maps.MapView 
  7.         android:id="@+id/mapview" 
  8.         android:layout_width="fill_parent" 
  9.         android:layout_height="fill_parent" 
  10.         android:apiKey="这里是你刚才获得的KEY" 
  11.         android:clickable="true" /> 
  12.  
  13. </RelativeLayout>  

 

 
 
  1. package com.billhoo.study.hellogooglemaps; 
  2.  
  3. import android.os.Bundle; 
  4.  
  5. import com.google.android.maps.MapActivity; 
  6. import com.google.android.maps.MapView; 
  7.  
  8. public class MainActivity extends MapActivity { 
  9.     MapView mMapView = null
  10.      
  11.     @Override 
  12.     public void onCreate(Bundle savedInstanceState) { 
  13.         super.onCreate(savedInstanceState); 
  14.         setContentView(R.layout.activity_main); 
  15.         mMapView = (MapView) findViewById(R.id.mapview); 
  16.         mMapView.setBuiltInZoomControls(true); // 使用內置放大縮小控制按钮 
  17.     } 
  18.      
  19.     @Override 
  20.     protected boolean isRouteDisplayed() { 
  21.         return false
  22.     } 

    到此为止,在Android上使用Google Map的基本工作就算完成了。

    当然,在Android上开发GoogleMap相关应用还有许多难点,bill会慢慢将这次开发中遇到的难题及其解决方案写成博文分享给还不会的朋友。

    




     本文转自Bill_Hoo 51CTO博客,原文链接:http://blog.51cto.com/billhoo/967254,如需转载请自行联系原作者





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值