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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package irdc.ex10_06;

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

import android.content.Intent;
import android.database.Cursor;
import android.graphics.RectF;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

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 com.google.android.maps.Overlay;

public class EX10_06_04<MapLocation> extends MapActivity
{
  private TextView mTextView01;
  /* 獨一無二的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;
  
  /* Google地圖所需成員變數 */
  private MapView mMapView01;
  private int intZoomLevel=20;
  
  /* 資料庫所需成員變數 */
  private MySQLiteOpenHelper dbHelper=null;
  private int version = 1;
  private List<String> allRestaurantID;
  private List<String> allRestaurantName;
  private List<String> allRestaurantAddress;
  private List<String> allRestaurantCal;
  
  /* 資料庫資料表 */
  private String tables[] = { "t_restaurant" };
  
  /* 資料庫欄位名稱 */
  private String fieldNames[][] =
  {
    { "f_id", "f_name", "f_address", "f_cal" }
  };
  
  /* 資料庫欄位資料型態 */
  private String fieldTypes[][] =
  {
    { "INTEGER PRIMARY KEY AUTOINCREMENT", "text" , "text", "text"}
  };
  private EditText mEditText05;
  private Button mButton02;
  private Object selectedMapLocation;
  private RectF bubbleIcon;
  
  @Override
  protected void onCreate(Bundle savedInstanceState)
  { 
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_draw);


    mTextView01 = (TextView)findViewById(R.id.myTextView7);
    /* 建立MapView物件 */
    mMapView01 = (MapView)findViewById(R.id.myMapView1);
    
    /* 資料庫連線 */
    dbHelper = new MySQLiteOpenHelper(this, "mydb", null, version, tables, fieldNames, fieldTypes);
    
    /* 系統選擇餐館 */
    drawRestaurant();
    mEditText05 = (EditText)findViewById(R.id.destination);
    mEditText05.setText 
     (getResources().getText(R.string.str_default_address).toString()); 
     mMapView01 = (MapView)findViewById(R.id.myMapView1);
    // mMapView01.setTraffic(true);
     mButton02 = (Button)findViewById(R.id.myButton2);
     mButton02.setOnClickListener(new Button.OnClickListener()
     {
      
       public void onClick(View v)
       {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);
         }
       }
     });
   }

  @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 onMenuItemSelected(int featureId, MenuItem item)
  {
    // TODO Auto-generated method stub
    Intent intent = new Intent();
    switch(item.getItemId())
    {
      case (MENU_ADD):
        /* 新建餐廳資料 */
        intent.setClass(EX10_06_04.this, EX10_06_02.class);
        startActivity(intent);
        finish();
        break;
      case (MENU_EDIT):
        /* 編輯餐廳資料 */
        intent.setClass(EX10_06_04.this, EX10_06_03.class);
        startActivity(intent);
        finish();
        break;
      case (MENU_DRAW):
        /* 今天吃什麼? */
        drawRestaurant();
        break;
    }
    return super.onMenuItemSelected(featureId, item);
  }
  
  /* 取得餐廳資料函數 */
  private void drawRestaurant()
  { ArrayList<MapLocation> mapLocations = new ArrayList<MapLocation>(); 
    String f[] = { "f_id", "f_name", "f_address", "f_cal"};
    /* SELECT f[] FROM tables[0] */
    Cursor c = dbHelper.select(tables[0], f, "", null, null, null, null);
    allRestaurantID = new ArrayList<String>();
    allRestaurantName = new ArrayList<String>();
    allRestaurantAddress = new ArrayList<String>();
    allRestaurantCal = new ArrayList<String>();
    
    /* 將所有餐廳資料放入List<String>物件 */
    while (c.moveToNext())
    {
      allRestaurantID.add(c.getString(0));
      allRestaurantName.add(c.getString(1));
      allRestaurantAddress.add(c.getString(2));
      allRestaurantCal.add(c.getString(3));
    }
    
    if(allRestaurantID.size()>0)
    {
      //Random generator = new Random();
      //int intThrowIndex = generator.nextInt(allRestaurantID.size());
      for (int i=0;i<allRestaurantID.size();i++)
      {
        //顯示熱量地址名稱
        mTextView01.setText
        (
          allRestaurantName.get(i)+"\n"+
          allRestaurantAddress.get(i)+"\n"+
          allRestaurantCal.get(i)+
          getResources().getText(R.string.str_cal)
        );
      
      /* 以地址查詢地理座標 */
      GeoPoint gp = getGeoByAddress(allRestaurantAddress.get(i));
      if(gp==null)
      {
        /* 地址無法反查為GeoPoint時 */
        mMapView01.setVisibility(MapView.GONE);
      }
      else
      { 
        
        /* 更新MapView地圖 */
        mMapView01.setVisibility(MapView.VISIBLE);
        showImageOverlay(gp);
        //取得位址後更新顯示
        refreshMapViewByGeoPoint(getGeoByAddress(allRestaurantAddress.get(0)), mMapView01, intZoomLevel, true);
      }
     }
    }
    else
    {
      /* 資料庫無紀錄 */
      
    }
  }
  
  /**
   * 查詢地址的地理座標 
   * @param strSearchAddress 地址字串
   * @return GeoPoint 地理座標物件
   */
  private GeoPoint getGeoByAddress(String strSearchAddress)
  {
    GeoPoint gp = null;
    try
    {
      if(strSearchAddress!="")
      {
        Geocoder mGeocoder01 = new Geocoder(EX10_06_04.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;
  }
  
  /**
   * 更新MapView地圖
   * @param gp GeoPoint地理座標物件
   * @param mv 查詢的資料的欄位名稱
   * @param zoomLevel 放大層級
   * @param setSatellite 是否顯示衛星地圖
   */
  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();
    }
  }
  
  /**
   * 在地圖上顯示Overlay圖片
   * @param gp GeoPoint地理座標物件
   */
  private void showImageOverlay(GeoPoint gp)
  {
    /* 設定Overlay*/
    GeoPointImageOverlay mLocationOverlay01;
    mLocationOverlay01 = new GeoPointImageOverlay(gp,R.drawable.bubble);
    List<Overlay> overlays = mMapView01.getOverlays();
    overlays.add(mLocationOverlay01);
  }
  
  @Override
  protected boolean isRouteDisplayed()
  {
    // TODO Auto-generated method stub
    return false;
  }


 
}