编写这个程序之前需要有一个Google
地图
扩展库在SDK中,一般来说,android 1.5中就已经有了。如果没有的话可以在这里下载:
http://code.google.com/android/add-ons/google-apis
这个 插件 要求android 1.5或者更高的版本。下载后按照自己的环境做相应的调整。
第一步:先建立新的activity名字叫 Android MapView;
第五步:打开Java文件。即AndroidMapView.java文件。在这个activity中我们继承了一个特别的类叫做:MapActivity 。
因此将程序开头改成如下形式:
public class AndroidMapView extends MapActivity {
第六步:这里需要一个名叫isRouteDisplayed()的方法,这个方法是必须的。用来获取地图API密匙,一旦获得就会传给上边提到的apiKey这个变量。
Android中提供了一个叫ZoomControls的 控件 对地图来进行放大和缩小处理。
第七步:回到配置文件中来,我们需要一个新的ViewGroup布局器来放置ZoomControls这个控件。
第八步:回到AndroidMapView.java文件中。
第九步: onCreate()在这个方法中将第八部中定义的变量全部初始化。
第十步:将控件ZoomControls添加到LinearLayout中去。
main.xml文件的源码
AndroiMaifest.xml文件的源码
转贴: http://www.pin5i.com/showtopic-android-google-maps-development.html
http://code.google.com/android/add-ons/google-apis
这个 插件 要求android 1.5或者更高的版本。下载后按照自己的环境做相应的调整。
第一步:先建立新的activity名字叫 Android MapView;
第二步:因为我是用的是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>
转贴: http://www.pin5i.com/showtopic-android-google-maps-development.html