编写这个程序之前需要有一个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>

更多相关文章

  1. android中一些配置文件的参数的意义
  2. Android实现文件上传功能
  3. android Gradle打包修改生成的apk文件名称
  4. Android中遍历文件夹、比较文件类型测试
  5. android百度地图标记点代码
  6. Android存储数据到本地文件
  7. Android 上传文件到XP
  8. Android使用代码模拟HOME键的功能

随机推荐

  1. Docker —— 简介与镜像用法
  2. 网站搭建系列:云虚拟主机建站部署的过程中
  3. 商品和oss
  4. cleanmymac x激活码(共享激活码秘钥)永久使
  5. 自定义方法通过类名获取对象集合
  6. 模拟数组pop,push,toString和冒泡排序(sor
  7. 如何绘制背包?不同角度的背包画法教程!
  8. 输入密码动画开门效果
  9. Manjaro Linux 入门使用教程
  10. 意派Epub360丨年会攻略丨轻松get超火爆的