要實作前請先看前置作業http://cookiesp.pixnet.net/blog/post/77243100

這個程式分了兩個java檔welcome.java跟GoogleMapActivity
所有的文字檔都在value下的string.xml

layout有3個 

welcom.xml是welcome.java的 

main.xml是GoogleMapActivity.java的

alert.xml是GoogleMapActivity裡設定經緯度的AlertDialog的

附上APK檔(只能在android emulator裡用 因為GoogleMap金鑰是只限測試用)  http://dl.dropbox.com/u/23148579/EasyMap.apk 

先看結果

EasyMap_1   EasyMap_2  

EasyMap_3   EasyMap_4  

EasyMap_5   EasyMap_6  

EasyMap_7  

下面是主程式程式碼 welcome很簡單 就是一個imageView按下去會切換到GoogleMapActivity而已 就不貼了

string.xml就是字串而已 也不貼了

==========================================================GoogleMapActivity

package com.map;

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.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ZoomControls;

public class GoogleMapActivity extends MapActivity {
    private MapView mapView;
    private MapController mapController;
    private GeoPoint geoPoint;
    static final private int MENU_CHANGE_COUNTY=Menu.FIRST;
    static final private int MENU_CHANGE_LOCATION=Menu.FIRST+1;
    static final private int MENU_ABOUT=Menu.FIRST+2;
    static final private int MENU_EXIT=Menu.FIRST+3;
    private EditText input_longitude;//經度
    private EditText input_latitude;//緯度
    private ZoomControls zoomControls;//變焦控制鈕
    private int zoom_range=18;//預設變焦大小
    private CheckBox checkBoxL,checkBoxR;//變換交通,衛星
    private int whichCounty;
    private String[] countyItem;//台灣縣市
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        countyItem=new String[24];
        countyItem[0]=getString(R.string.county_1);
        countyItem[1]=getString(R.string.county_2);
        countyItem[2]=getString(R.string.county_3);
        countyItem[3]=getString(R.string.county_4);
        countyItem[4]=getString(R.string.county_5);
        countyItem[5]=getString(R.string.county_6);
        countyItem[6]=getString(R.string.county_7);
        countyItem[7]=getString(R.string.county_8);
        countyItem[8]=getString(R.string.county_9);
        countyItem[9]=getString(R.string.county_10);
        countyItem[10]=getString(R.string.county_11);
        countyItem[11]=getString(R.string.county_12);
        countyItem[12]=getString(R.string.county_13);
        countyItem[13]=getString(R.string.county_14);
        countyItem[14]=getString(R.string.county_15);
        countyItem[15]=getString(R.string.county_16);
        countyItem[16]=getString(R.string.county_17);
        countyItem[17]=getString(R.string.county_18);
        countyItem[18]=getString(R.string.county_19);
        countyItem[19]=getString(R.string.county_20);
        countyItem[20]=getString(R.string.county_21);
        countyItem[21]=getString(R.string.county_22);
        countyItem[22]=getString(R.string.county_23);
        countyItem[23]=getString(R.string.county_24);
        Toast.makeText(GoogleMapActivity.this,getString(R.string.tip),Toast.LENGTH_LONG).show();
        
        mapView = (MapView) findViewById(R.id.gmap);
        zoomControls=(ZoomControls) findViewById(R.id.zoomControls);
        checkBoxL=(CheckBox) findViewById(R.id.checkBoxL);
        checkBoxR=(CheckBox) findViewById(R.id.checkBoxR);
        mapController = mapView.getController();
        //設定交通圖
        mapView.setTraffic(false);
        //設定衛���圖
        mapView.setSatellite(false);
        //設定街道圖
        mapView.setStreetView(true);
        //設定地圖級距1-20
        mapController.setZoom(zoom_range);
        //一開始設定台北101的座標
        geoPoint = new GeoPoint((int)(25.035527 * 1E6), (int)(121.56415 * 1E6));
        mapView.displayZoomControls(true);
        /* 將Map的中點移至GeoPoint */
        mapController.animateTo(geoPoint);
        
        //設定變焦
        zoomControls.setOnZoomInClickListener(new ZoomControls.OnClickListener(){
            @Override
            public void onClick(View arg0) {
                if(zoom_range==20){
                    Toast.makeText(GoogleMapActivity.this,getString(R.string.zoom_toast_21),Toast.LENGTH_SHORT).show();
                }else{
                    zoom_range+=1;
                    mapController.setZoom(zoom_range);
                }
            }
        });
        zoomControls.setOnZoomOutClickListener(new ZoomControls.OnClickListener(){
            @Override
            public void onClick(View arg0) {
                if(zoom_range==1){
                    Toast.makeText(GoogleMapActivity.this,getString(R.string.zoom_toast_1),Toast.LENGTH_SHORT).show();
                }else{
                    zoom_range-=1;
                    mapController.setZoom(zoom_range);
                }
            }
        });
        
        //設定checkBox街道與衛星
        checkBoxL.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){//交通
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                if(checkBoxL.isChecked()==false){//如果呈現off狀態
                    mapView.setTraffic(false);
                }else{
                    mapView.setTraffic(true);;
                }
            }
        });
        checkBoxR.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){//衛星
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                if(checkBoxR.isChecked()==false){//如果呈現off狀態
                    mapView.setSatellite(false);;
                }else{
                    mapView.setSatellite(true);;
                }
                
            }
        });
        
    }
    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
    public boolean onCreateOptionsMenu(Menu menu){
        menu.add(0,MENU_CHANGE_COUNTY,0,getString(R.string.menu_1));
        menu.add(0,MENU_CHANGE_LOCATION,1,getString(R.string.menu_2));
        menu.add(0,MENU_ABOUT,2,getString(R.string.menu_3));
        menu.add(0,MENU_EXIT,3,getString(R.string.menu_4));
        
        return super.onCreateOptionsMenu(menu);
    }
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
        case (MENU_CHANGE_COUNTY):
            new AlertDialog.Builder(GoogleMapActivity.this)
            .setTitle(getString(R.string.change_county_title))
            .setSingleChoiceItems(countyItem,0 ,new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int which) {
                    whichCounty=which;
                }
            }).setPositiveButton(getString(R.string.change_county_L),new DialogInterface.OnClickListener() {//左按鍵確定
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    switch(whichCounty+1){
                    case 1://基隆市
                        geoPoint = new GeoPoint((int)(25.115445 * 1E6), (int)(121.707916 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 2://新北市
                        geoPoint = new GeoPoint((int)(24.946219 * 1E6), (int)(121.676331 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 3://台北市
                        geoPoint = new GeoPoint((int)(25.097103 * 1E6), (int)(121.559601 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 4://桃園縣
                        geoPoint = new GeoPoint((int)(24.9512 * 1E6), (int)(121.218338 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 5://新竹縣
                        geoPoint = new GeoPoint((int)(24.717519 * 1E6), (int)(121.125641 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 6://宜蘭縣
                        geoPoint = new GeoPoint((int)(24.70941 * 1E6), (int)(121.716156 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 7://苗栗縣
                        geoPoint = new GeoPoint((int)(24.503395 * 1E6), (int)(120.942993 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 8://台中縣
                        geoPoint = new GeoPoint((int)(24.259485 * 1E6), (int)(120.94574 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 9://台中市
                        geoPoint = new GeoPoint((int)(24.259485 * 1E6), (int)(120.94574 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 10://南投縣
                        geoPoint = new GeoPoint((int)(23.863234 * 1E6), (int)(120.991058 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 11://花蓮縣
                        geoPoint = new GeoPoint((int)(23.784088 * 1E6), (int)(121.360474 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 12://彰化縣
                        geoPoint = new GeoPoint((int)(24.009462 * 1E6), (int)(120.482254 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 13://雲林縣
                        geoPoint = new GeoPoint((int)(23.769007 * 1E6), (int)(120.387497 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 14://嘉義縣
                        geoPoint = new GeoPoint((int)(23.487179 * 1E6), (int)(120.574951 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 15://台南縣
                        geoPoint = new GeoPoint((int)(23.171926 * 1E6), (int)(120.250854 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 16://台南市
                        geoPoint = new GeoPoint((int)(23.171926 * 1E6), (int)(120.250854 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 17://高雄縣
                        geoPoint = new GeoPoint((int)(23.031715 * 1E6), (int)(120.665588 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 18://高雄市
                        geoPoint = new GeoPoint((int)(23.031715 * 1E6), (int)(120.665588 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 19://台東縣
                        geoPoint = new GeoPoint((int)(23.051935 * 1E6), (int)(120.984192 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 20://屏東縣
                        geoPoint = new GeoPoint((int)(22.579779 * 1E6), (int)(120.621643 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 21://澎湖縣
                        geoPoint = new GeoPoint((int)(23.590419 * 1E6), (int)(119.606781 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 22://綠島
                        geoPoint = new GeoPoint((int)(22.659958 * 1E6), (int)(121.485615 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 23://小琉球
                        geoPoint = new GeoPoint((int)(22.339398 * 1E6), (int)(120.369902 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    case 24://蘭嶼
                        geoPoint = new GeoPoint((int)(22.047777 * 1E6), (int)(121.548443 * 1E6));
                        mapController.animateTo(geoPoint);
                        break;
                    }
                    
                }
            }).setNegativeButton(getString(R.string.change_county_R),new DialogInterface.OnClickListener() {//右按鍵取消
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                }
            }).show();
            
            break;
        case (MENU_CHANGE_LOCATION):
            
            //-----------取得alert Layout reference--------
            LayoutInflater inflater = LayoutInflater.from(GoogleMapActivity.this);        
            View alert_view = inflater.inflate(R.layout.alert,null);
            //-----------產生輸入視窗--------        
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
             builder.setTitle(getString(R.string.alert_title));
             builder.setMessage(getString(R.string.alert_msg));
             builder.setView(alert_view); 
         
             input_longitude=(EditText)alert_view.findViewById(R.id.longitude);
             input_latitude=(EditText)alert_view.findViewById(R.id.latitude);
             final AlertDialog dialog = builder.setPositiveButton(getString(R.string.alert_button_L),
                     new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface arg0, int arg1) {
                  //如果未輸入提示使用者輸入
                  try{
                      geoPoint = new GeoPoint((int)(Double.parseDouble(input_longitude.getText().toString()) * 1E6)
                              , (int)(Double.parseDouble(input_latitude.getText().toString())* 1E6));
                      mapController.animateTo(geoPoint);
                  }catch(Exception e){
                      Toast.makeText(GoogleMapActivity.this,getString(R.string.alert_tip),Toast.LENGTH_SHORT).show();
                  }
              }
            }).setNegativeButton(getString(R.string.alert_button_R),new DialogInterface.OnClickListener() {
            
              @Override
              public void onClick(DialogInterface arg0, int arg1) {
              }
            }).create();         
             dialog.show();
         
            break;
        case (MENU_ABOUT):
            new AlertDialog.Builder(GoogleMapActivity.this)
            .setTitle(getString(R.string.menu_3))
            .setMessage(getString(R.string.about)).show();
            break;
        case (MENU_EXIT):
            GoogleMapActivity.this.finish();
            break;
        }
        return super.onOptionsItemSelected(item);
    }
}
=================================================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"
    android:weightSum="1">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/title"
    />
     <com.google.android.maps.MapView
        android:id="@+id/gmap"
        android:apiKey="0uMWrBmlZ0NjS6KQT4mAcV_jNCnP6CH_R-zymbQ"
        android:layout_width="fill_parent"
        android:clickable="true" android:layout_height="346dp" android:layout_weight="0.84"/>
     <RelativeLayout android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="match_parent">
     <ZoomControls android:layout_height="wrap_content" android:id="@+id/zoomControls" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentRight="true"></ZoomControls>
     <CheckBox android:text="@string/checkBoxR" android:id="@+id/checkBoxR" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_toLeftOf="@+id/zoomControls"></CheckBox>
     <CheckBox android:text="@string/checkBoxL" android:id="@+id/checkBoxL" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_toLeftOf="@+id/checkBoxR"></CheckBox>
     </RelativeLayout>
     
     
</LinearLayout>
=======================================================alert.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="match_parent">
        <TextView android:layout_height="wrap_content" 
         
        android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView1" android:text="@string/alert_latitude" android:layout_width="wrap_content"></TextView>
        <EditText android:layout_weight="1" 
        android:layout_width="220px"
        android:textSize="18sp" 
        android:layout_height="wrap_content" android:id="@+id/longitude" android:inputType="numberDecimal" ></EditText>
    </LinearLayout>
    <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="match_parent">
        <TextView android:layout_height="wrap_content" 
         
        android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView1" android:text="@string/alert_longitude" android:layout_width="wrap_content"></TextView>
        <EditText android:layout_weight="1" 
        android:layout_width="220px"
        android:textSize="18sp" 
        android:layout_height="wrap_content" android:id="@+id/latitude" android:inputType="numberDecimal" ></EditText>
    </LinearLayout>
    
    
</LinearLayout>
===============================================================

文章標籤
創作者介紹

Mazs's Notes

cookiesp 發表在 痞客邦 PIXNET 留言(0) 人氣()