google map初级尝试_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > google map初级尝试

google map初级尝试

 2011/1/17 7:40:02  gundumw100  http://gundumw100.javaeye.com  我要评论(0)
  • 摘要:新建的是googlemap工程packagecom.wt.app;importandroid.os.Bundle;importcom.google.android.maps.GeoPoint;importcom.google.android.maps.MapActivity;importcom.google.android.maps.MapController;importcom.google.android.maps.MapView;importandroid.app.AlertDialog
  • 标签:Google Map
新建的是google map 工程
package com.wt.app;

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 App extends MapActivity {
	private static final String TAG="App";
	// 地图显示控制相关变量定义
	private MapView map=null;
	private MapController mapCon;
	
	// 菜单项
	final private int menuMode=Menu.FIRST;
	final private int menuExit=Menu.FIRST+1;
	private int chooseItem=0;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // 获取MapView
        map=(MapView)findViewById(R.id.map);
        // 设置显示模式
        map.setTraffic(true);
//        map.setSatellite(true);
//        map.setStreetView(true);
        // 设置可以缩放
        map.setBuiltInZoomControls(true);
        
        // 设置初始地图的中心位置
        GeoPoint geoBeijing=new GeoPoint((int)(39.95*1000000), (int)(116.37*1000000));
        mapCon=map.getController();
        mapCon.setCenter(geoBeijing);
        mapCon.setZoom(12);//1-21
    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// 建立菜单
    	menu.add(0,menuMode,0,"地图模式");
		menu.add(0, menuExit, 1, "退出");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case menuExit:
			finish();
			break;
		case menuMode:
			Dialog dMode=new AlertDialog.Builder(this)
			.setTitle("地图模式设置")
			.setSingleChoiceItems(R.array.MapMode, chooseItem, new DialogInterface.OnClickListener()
			{
				public void onClick(DialogInterface dialog, int which) {
					chooseItem=which;
				}
			})
			.setPositiveButton("确定", new DialogInterface.OnClickListener()
			{
				public void onClick(DialogInterface dialog, int which) {
					Log.d(TAG,"which="+which);
					switch (which) {
					case 0:
						map.setSatellite(true);
						//map.setTraffic(false);
						//map.setStreetView(false);
						break;
					case 1:
						//map.setSatellite(false);
						map.setTraffic(true);
						//map.setStreetView(false);
						break;
					case 2:
						//map.setSatellite(false);
						//map.setTraffic(false);
						map.setStreetView(true);
						break;
					default:
						break;
					}
				}
			})
			.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					
				}
			})
			.create();
			dMode.show();
			break;
		default:
			break;
		}
		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    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="089ERVJSRYdnecGr07_l4K__MDmjUAC6ym6TRGA"
       />    
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.wt.app"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <uses-library android:name="com.google.android.maps" />
        <activity android:name=".App"
                  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-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-sdk android:minSdkVersion="3" />

</manifest> 




如果只显示网格线,请确认将模拟器的时间调到北京时间(东八区)。
发表评论
用户名: 匿名