1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package irdc.ex10_06;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;


public class EX10_06 extends Activity
{
  /* 獨一無二的menu選項identifier,用以識別事件 */
  static final private int MENU_ADD = Menu.FIRST;
  static final private int MENU_EDIT = Menu.FIRST+1;
  static final private int MENU_DRAW = Menu.FIRST+2;
  private MapView mMapView01;
  private EditText mEditText05;
  private int intZoomLevel=20;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mEditText05 = (EditText)findViewById(R.id.destination);
    mMapView01 = (MapView)findViewById(R.id.myMapView1);
  
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu)
  {
    // TODO Auto-generated method stub
    /* menu群組ID */
    int idGroup1 = 0;
    
    /* The order position of the item */
    int orderItem1 = Menu.NONE;
    int orderItem2 = Menu.NONE+1;
    int orderItem3 = Menu.NONE+2;
    
    /* 建立3個Menu選單 */
    menu.add(idGroup1, MENU_ADD, orderItem1, R.string.str_manu1).setIcon(android.R.drawable.ic_menu_add);
    menu.add(idGroup1, MENU_EDIT, orderItem2, R.string.str_manu2).setIcon(android.R.drawable.ic_dialog_info);
    menu.add(idGroup1, MENU_DRAW, orderItem3, R.string.str_manu3).setIcon(R.drawable.hipposmall);
    
    return super.onCreateOptionsMenu(menu);
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item)
  {
    // TODO Auto-generated method stub
    Intent intent = new Intent();
    switch(item.getItemId())
    {
      case (MENU_ADD):
        /* 新建餐廳資料 */
        intent.setClass(EX10_06.this, EX10_06_02.class);
        startActivity(intent);
        finish();
        break;
      case (MENU_EDIT):
        /* 編輯資料 */
        intent.setClass(EX10_06.this, EX10_06_03.class);
        startActivity(intent);
        finish();
        break;
      case (MENU_DRAW):
        /* 前往系統亂數選擇餐廳功能 */
        draw();
        break;
    }
    return super.onOptionsItemSelected(item);
  }
        
     

    private void draw()
    {
        
       GeoPoint gp = getGeoByAddress(mEditText05.getText().toString());
          if(gp==null)
          {
            /* 地址無法反查為GeoPoint時 */
            mMapView01.setVisibility(MapView.GONE);
          }
          else
          {
            /* 更新MapView地圖 */
            mMapView01.setVisibility(MapView.VISIBLE);
            
            //取得位址後更新顯示
            refreshMapViewByGeoPoint(getGeoByAddress(mEditText05.getText().toString()), mMapView01, intZoomLevel, true);
          }
  
    }

    public static void refreshMapViewByGeoPoint(GeoPoint gp, MapView mv, int zoomLevel, boolean setTraffic)
    {
      try
      {
        mv.setBuiltInZoomControls(true);
        mv.displayZoomControls(true);
        MapController mc = mv.getController();
        mc.animateTo(gp);
        mc.setZoom(zoomLevel);
        mv.setTraffic(setTraffic);
      }
      catch(Exception e)
      {
        e.printStackTrace();
      }
    }
    

        private GeoPoint getGeoByAddress(String strSearchAddress)
        {
          GeoPoint gp = null;
          try
          {
            if(strSearchAddress!="")
            {
              Geocoder mGeocoder01 = new Geocoder(EX10_06.this, Locale.getDefault());
              List<Address> lstAddress = mGeocoder01.getFromLocationName(strSearchAddress, 1);
              if (!lstAddress.isEmpty())
              {
                Address adsLocation = lstAddress.get(0);
                /* 1E6 = 1000000*/
                double geoLatitude = adsLocation.getLatitude()*1E6;
                double geoLongitude = adsLocation.getLongitude()*1E6;
                gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
              }
            }
          }
          catch (Exception e)
          { 
            e.printStackTrace(); 
          }
          return gp;
        }
     }