AlertDialog的菜单以及MapView使用

这个例子主要可以学习到:
1.使用AlertDialog显示菜单item以方便选择
2.利用mapView显示goole地图

下面直接把代码贴上:

package com.sun.GoogleMap1;

import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;

public class GoogleMap1 extends MapActivity {
	private final String TAG = "MapPrac";
	private MapView mapView = null;
	private MapController mc;
	private int chooseItem = 0;
	// Menu items
	final private int menuMode = Menu.FIRST;
	final private int menuExit = Menu.FIRST + 1;
	final private int menuCommandList = Menu.FIRST + 2;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mapView = (MapView) findViewById(R.id.map);
		mc = mapView.getController();

		mapView.setTraffic(true);
		mapView.setSatellite(true);

		// GeoPoint gp = new GeoPoint((int)(39.269259 * 1000000),
		// (int)(115.255762 * 1000000));
		// yixian

		GeoPoint gp = new GeoPoint((int) (39.95 * 1000000),
				(int) (116.37 * 1000000));// beijing

		// mc.animateTo(gp);
		// mc.setZoom(12);
		mc.setCenter(gp);
		// to display zoom control in MapView
		mapView.setBuiltInZoomControls(true);

	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub

		Log.i(TAG, "enter onKeyDown");
		return super.onKeyDown(keyCode, event);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, menuMode, 0, "Map Mode");
		menu.add(0, menuCommandList, 1, "Command List");
		menu.add(0, menuExit, 2, "Exit");

		return super.onCreateOptionsMenu(menu);

	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case menuMode:
Dialog dMode = new AlertDialog.Builder(this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.alert_dialog_single_choice)
.setSingleChoiceItems(R.array.select_dialog_items,
chooseItem, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {

chooseItem = whichButton;
}
})
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
switch (chooseItem) {
  case 0:
  mapView.setSatellite(true);
  break;
case 1:
mapView.setSatellite(true);
break;
case 2:
mapView.setTraffic(true);
break;
case 3:
mapView.setStreetView(true);
break;
default:
break;									}
}
})
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
										int whichButton) {
}
}).create();
dMode.show();
break;
case menuCommandList:
// create the dialog
Dialog d = new AlertDialog.Builder(this)
.setTitle(R.string.select_dialog)
.setItems(R.array.select_dialog_items,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
/* User clicked so do some stuff */
String[] items = getResources()
.getStringArray(
R.array.select_dialog_items);
}
}).create();
// show the dialog
d.show();

break;

case menuExit:
finish();
break;
default:
break;
}
return super.onMenuItemSelected(featureId, item);
	}

}



这个String.xml也要贴下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, GoogleMap1!</string>
    <string name="app_name">GoogleMap1</string>
    <string name="select_dialog">select_dialog</string>
    <string name="alert_dialog_ok">ok</string>
    <string name="alert_dialog_cancel">Cancel</string>
    <string name="alert_dialog_single_choice">Map Mode List</string>
    
    <string-array name="select_dialog_items">
        <item>Map</item>
        <item>Satellite</item>
        <item>Traffic</item>
        <item>Street View</item>
    </string-array>
</resources>


因为里面用到了string-array这个属性,我们可以将这个数组列表显示在比如:
AlertDialog当中

最后贴下main.xml,这个就是一个简单的MapView,我们只要申请个API-key就可以了:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<com.google.android.maps.MapView
        android:id="@+id/map"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:enabled="true"
        android:clickable="true"
        android:apiKey="0y_0zdWEE7CcFIM2QPL756MOF-FT981aiCzSrhA"
        />  
</LinearLayout>



最后还是加下AndroidMenifest.xml吧
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.sun.GoogleMap1"
      android:versionCode="1"
      android:versionName="1.0">
    
      
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".GoogleMap1"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <uses-library android:name="com.google.android.maps" />
    </application>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-sdk android:minSdkVersion="3" />
</manifest> 

你可能感兴趣的:(android,xml,OS,Google,sun)