这是MapView的简单示例程序。使用MapView需要apiKey.关于如何得到apiKey请参阅《MapView的apiKey》
MainActivity.java文件
package
com.teleca.robin;
import
android.app.Activity;
import
android.content.Intent;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
public class
MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.Button1);
OnClickListener listener = new OnClickListener() {
public void
onClick(View v) {
goMap();
System.out.println("go Map");
}
};
button.setOnClickListener(listener);
}
void
goMap()
{
Intent intent=new Intent("
com.teleca.robin.intent.action.MyMapView
");
startActivity(intent);
}
}
MyMapActivity.java文件
package
com.teleca.robin;
import
com.google.android.maps.MapActivity;
import
android.os.Bundle;
public class
MyMapActivity extends
MapActivity
{
/** Called when the activity is first created. */
@Override
public void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
}
@Override
protected boolean
isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
注意
:使用
com.google.android.maps.MapView的Activity必须是
MapActivity或其子类
main.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:text="@string/goViewMap"
android:id="@+id/Button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
></Button>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
mapview.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<
com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="
00eTdGt9OWBDzzT0wQBPGYOnaS7pnd6dBj1bJjQ"
android:clickable="true"
android:focusable="true"
/>
</LinearLayout>
strings.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, MainActivity!</string>
<string name="app_name">SimpleMapDemo</string>
<string name="goViewMap">go to view map</string>
<string name="MyGoogleMap">My Google Map</string>
</resources>
AndroidManifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.teleca.robin" android:versionCode="1" android:versionName="1.0">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="
.MyMapActivity
" android:label="@string/MyGoogleMap">
<intent-filter>
<action android:name="com.teleca.robin.intent.action.MyMapView" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.maps" />
</application>
<uses-sdk android:minSdkVersion="9" />
</manifest>
注意1:必须要添加
android.permission.ACCESS_COARSE_LOCATION和
android.permission.INTERNET权限.
注意2:需要使用来
<uses-library android:name="com.google.android.maps" />什么声明使用
com.google.android.maps这个库。如果在Eclipse中,我们是手动把
com.google.android.maps包加载到编译路径中的话,请以“
Libirary”方式进行加载。即在android工程的
properties->
Java Build Path->Libiraray中以“
Libirary”方式进行加载,而不是“
add External JARs”进行加载。