编写这个程序之前需要有一个Google地图扩展库在SDK中,一般来说,android 1.5中就已经有了。如果没有的话可以在这里下载:
http://code.google.com/android/add-ons/google-apis
这个插件要求android 1.5或者更高的版本。下载后按照自己的环境做相应的调整。
第一步:先建立新的activity名字叫AndroidMapView;
第二步:因为我是用的是Google的地图库,而非Android标准库。所以我们先要在AndroidManifest.xml文件<application>中作出声明。 <uses-library android:name="com.google.android.maps" />
复制代码第三步:以为这个程序需要实时的从Google地图库中读取信息,所以添加接入Internet权限。在<manifest>中声明: <uses-permission android:name="android.permission.INTERNET" />
复制代码第四步:现在主要的配置文件都已经开放给我们。定义一个layout给com.google.android.maps.MapView其中包括android.widget.RelativeLayout : <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="Your Maps API Key"
/>
</RelativeLayout>
复制代码Clickable属性决定了用户能否和地图之间进行互动。一次设置成TRUE。apiKey属性中包含了Google地图API密钥,由于MapView使用的是Google地图数据,在此主要是为了获得地图数据。
第五步:打开Java文件。即AndroidMapView.java文件。在这个activity中我们继承了一个特别的类叫做:MapActivity 。
因此将程序开头改成如下形式:
public class AndroidMapView extends MapActivity {
第六步:这里需要一个名叫isRouteDisplayed()的方法,这个方法是必须的。用来获取地图API密匙,一旦获得就会传给上边提到的apiKey这个变量。
Android中提供了一个叫ZoomControls的控件对地图来进行放大和缩小处理。
第七步:回到配置文件中来,我们需要一个新的ViewGroup布局器来放置ZoomControls这个控件。 <LinearLayout
android:id="@+id/zoomview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/mapview"
android:layout_centerHorizontal="true"
/>
复制代码第八步:回到AndroidMapView.java文件中。 LinearLayout linearLayout;
MapView mapView;
ZoomControls mZoom;
复制代码第九步: onCreate()在这个方法中将第八部中定义的变量全部初始化。 linearLayout = (LinearLayout) findViewById(R.id.zoomview);
mapView = (MapView) findViewById(R.id.mapview);
mZoom = (ZoomControls) mapView.getZoomControls();
复制代码第十步:将控件ZoomControls添加到LinearLayout中去。 linearLayout.addView(mZoom);
复制代码第十一步:到这我们就可以运行这个程序了。
需要注意的是红色字体部分,很有肯能忘记了填写apikey。如果没有可以参照这一篇《Android开发如何利用Google Maps》自己申请一个。
AndroidMapView.java文件的源码 package android.mapview;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.ZoomControls;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
public class AndroidMapView extends MapActivity {
/** Called when the activity is first created. */
LinearLayout linearLayout;
MapView mapView;
ZoomControls mZoom;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
linearLayout = (LinearLayout) findViewById(R.id.zoomview);
mapView = (MapView) findViewById(R.id.mapview);
mZoom = (ZoomControls) mapView.getZoomControls();
linearLayout.addView(mZoom);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
复制代码main.xml文件的源码 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="your api key"
/>
<LinearLayout
android:id="@+id/zoomview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/mapview"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
复制代码AndroiMaifest.xml文件的源码 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.mapview"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/map" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".AndroidMapView"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
分享到:
相关推荐
android google地图开发 android google地图开发 android google地图开发 android google地图开发 android google地图开发
android谷歌地图开发
android Google地图开发源程序,主要功能包括根据地名定位到该地,输入两个地名画出两地间的路径
很详细的android Google地图开发文档
android使用谷歌地图(Android uses Google Maps),利用高德地图加载瓦片图层(Using Gaud map to load tile layer)
谷歌地图开发,谷歌地图开发,谷歌地图开发
基于Google Map 开发android手机地图,通过GPS定位
android手机google地图开发时与实际地图上经纬度的转换问题.docx
基于android的google手机地图的开发
通过实例介绍了Android UI布局、Android人机界面、手机硬件设备的使用、Android本地存储系统、Android中的数据库、多线程设计、Android传感器、Android游戏开发基础、Android与Internet,以及Google地图服务等内容。...
C#开发Android 谷歌地图完整源码(使用Mono技术基于vs2010开发)
基于android的应用开发 Google地图应用开发
google地图SKD使用和google定位SDK使用
一个简单的Android实现GoogleMapApi v2展示Demo,注意:需下载安装Google Play ,需自己在google官方申请地图API Key, 需依赖google-play-services_lib作为类库(google-play-services_lib已有)
这是参照百度官方指导做的一个DEMO,其实百度地图的SDK比google地图开发起来要方便很多
第1章 Android开发简介 1.1 Android基本概念 1.1.1 Android简介 1.1.2 Android的系统构架 1.1.3 Android应用程序框架 1.2 OMS介绍 1.2.1 OPhone介绍 1.2.2 Widget介绍 1.3 小结 第2章 Android开发环境搭建 2.1 ...
android地图开发及google地图密钥申请.pdf
·谷歌地图(5.12.1,不是最新版,最新版是6.0.1,可安装完以后用电子市场升级) ·谷歌纵横 ·谷歌导航 ·谷歌搜索 ·谷歌本地搜索 ·谷歌语音(不是最新版,可安装完以后用电子市场升级) ·谷歌...
一、申请google Maps API key(用于开发和debug) 二.Google Map API的使用 三.实例开发
精心收集的android googlemap开发实例与教程,调用gps定位,以及给定经纬度显示地图信息等示例代码