顯示具有 Google Map 標籤的文章。 顯示所有文章
顯示具有 Google Map 標籤的文章。 顯示所有文章

2012年4月5日 星期四

[TQC+ Android] 3-6 查詢地圖 Use MapView , Google Map API 2.0





設計查詢地圖 , 可輸入想查詢的經緯度 , 於輸入後點選查詢 , 可顯示欲查詢地圖並可放大縮小地圖 , 這題的重點是在申請 Android Maps API Key , 過程是先從開發環境得電腦中取得Keystore 的MD5編碼 , 再透過Google網站簽暑便可取得了 , 另外因為必須使用到Google API 所以在建立Android 專案時 , Build target 記得選擇有Google API。


以下是取得Android Maps API Key的步驟:


1. 至開發環境 Eclipse IDE 中
    Window > Preferences > Android > Build > Default debug keystore 取得 keystore的存放路徑








2. 進入Dos模式 . 將目錄切換至JAVA資料夾下的\jre\bin, 範例路徑: C:\Java\jdk1.6.0\17\jre\bin
   (視電腦安裝路徑而定) , 然後在目錄下輸入以下指令 , 取得MD5編碼。


keytool -list -alias androiddebugkey -keystore "第一步驟的keystore的存放路徑" -storepass android -keypass android






3. 指令執行成功之後 , 會產生 keystore 檔案 , 並可取得一組MD5編碼 , 然後至以下網址輸入MD5編碼 , 取得Android Maps API Key。


https://code.google.com/intl/zh-TW/android/maps-api-signup.html




圖片中最下面那段xml配置範例是之後要加入到MapView的android:apiKey屬性值中的。


接下來我們來看看程式碼的部分。


package COM.TQC.GDD03;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapController; 
import com.google.android.maps.MapView; 


public class GDD03 extends MapActivity 
{
  private MapController MapController; 
  private MapView MapView;
  private Button btn_query, btn_zoomIn, btn_zoomOut; 
  private EditText EditLongitude; //經度
  private EditText EditLatitude; //緯度
  private int intZoomLevel=0;
  
  private double dLng=121.228626;//預設經度
  private double dLat=25.0402226; //預設緯度
  

  @Override 
  protected void onCreate(Bundle icicle) 
  { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 

    EditLongitude = (EditText) this.findViewById(R.id.editText1);
    EditLongitude.setText(dLng+"");
    EditLatitude = (EditText) this.findViewById(R.id.editText2);
    EditLatitude.setText(dLat+"");
    
    intZoomLevel = 15;
    
    MapView = (MapView) findViewById(R.id.mapview);    
    
    //MapView.setBuiltInZoomControls(false);
    //參數為True時 , 使用者可以直接控制MapView的縮放大小 
    //在這邊使用false讓使用者使用我們的按鈕來控制MapView的縮放大小
    
    MapView.setStreetView(true);
    //顯示街道圖
    MapController = MapView.getController();
 refreshMapView();    
    
 btn_query = (Button)findViewById(R.id.btn_query); 
    btn_query.setOnClickListener(new Button.OnClickListener() 
    { 
      //@Override
      public void onClick(View v) 
      {        
        if(EditLongitude.getText().toString().equals("")||
           EditLatitude.getText().toString().equals(""))
        {
          showDialog("經度或緯度填寫不正確!");
        }
        else
        {        
         showDialog("顯示地圖");
         refreshMapView(); 
        }
      } 
    });      
    
    btn_zoomIn = (Button)findViewById(R.id.btn_zoomIn); 
    btn_zoomIn.setOnClickListener(new Button.OnClickListener() 
    { 
      //@Override 
      public void onClick(View v) 
      { 
       intZoomLevel++;
       MapController.setZoom(intZoomLevel);    
      } 
    });      
    
    btn_zoomOut = (Button)findViewById(R.id.btn_zoomOut); 
    btn_zoomOut.setOnClickListener(new Button.OnClickListener() 
    { 
      //@Override
      public void onClick(View v) 
      { 
       intZoomLevel--;    
       MapController.setZoom(intZoomLevel);
      } 
    });
  } 
  
  GeoPoint GPoint;  
  
  public void refreshMapView() 
  {      
     dLng = Double.parseDouble(EditLongitude.getText().toString());
     dLat = Double.parseDouble(EditLatitude.getText().toString());
     GPoint = new GeoPoint((int)(dLat*1E6),(int)(dLng*1E6));
     //GeoPoint GPoint = new GeoPoint(19240000,-99120000);
     MapController.setCenter(GPoint);
     //將MapView的中心點定位在GPoint的位置
     MapController.setZoom(intZoomLevel);
    //設定地圖縮放比例
     MapView.invalidate(); 
  } 
   
  @Override 
  protected boolean isRouteDisplayed() 
  { 
    return false;
    //為了計算目的的需要 , 要讓Server知道我們是否正在顯示相關的路由資訊
    //在這個應用中是用不到 , 所以我們回傳false
  }
    
  private void showDialog(String mess)
  {
    new AlertDialog.Builder(GDD03.this).setTitle("Message")
    .setMessage(mess)
    .setNegativeButton("確定", new DialogInterface.OnClickListener()
    {
      public void onClick(DialogInterface dialog, int which)
      {}
    })
    .show();
  }
} 




由於MapActivity class 不在Android 官方內建的API中 , 我們必須在AndroidManifest.xml中引入 library : com.google.android.maps , 另外我們會存取使用者的當前座標所以還必須註冊相關的 use-permission 。

    
    
    
       
        
        
            
                
                
            
        
          
    
    
 




再來看看layout檔案

    






先前提到MapActivity class 不是Android 官方的API , 所以在官方的開發函式文件中是找不到相關資料的 , 下面這個網站可取得相關的資料。


Google Map APIs




P.S. 題目中所要求的Variable和Method皆會保留 , 也會根據題目所要求的流程去實作 , 縱使題目要求繞遠路....







2010年11月23日 星期二

[Android] 如何使用Google Map 2.0 & 3.0



接續之前的文章 [Android Tips] 如何取得手機的Cell ID 和 Location Area Code , 在我們獲得Cell ID 和 Location Area Code 之後 , 轉譯成我們要的地理座標呢? 這時候需要連上Google Map API接收他轉譯好的經緯度資訊 , 網址為: http://www.google.com/glm/mmap 點進去看到錯誤訊息不要以為他壞掉了 , 其實關於如何夠過上面的網址把Cell ID 和 Location Area Code轉譯成經緯度的資訊  Wei-Meng Lee 和 poor man 的文章說得很清處 , 流程大概就是連接上網頁POST丟入參數然後解析網頁回傳的資訊。


在成功的獲得經緯度資訊後 , 就可以拿他們去呼叫Google Map定位了 , 在Google Map 3.0推出之後 , 官網呼籲開發者把2.0的版本改成3.0的 , 3.0 載入速度更快 , 尤其是在行動裝置的瀏覽器上(但是我用模擬器測試反應倒是有點慢....)


其實這次3.0的作法是希望把API放在伺服器上(有別於2.0的原生API) , 這樣開發者就不必因為Map版本得更心要手動去更新Code , 所以Google想要開發者直接把Google Map的網頁嵌入在程式內 , 所以!!! 到了3.0的版本 , 裝載Google Map的容器換成是WebView了(有別於2.0使用MapView) , 而他們的官方網頁有簡單的教學 。


接下來是2.0的部分 , 其實也不用介紹太多 , 很多網站和書籍都有範例 , 像Google 的Andorid 開發者網站就有這兩篇文章Google Map View 和 Location and Maps , Google Map API 2.0 是需要申請金鑰的 , 關於這個步驟這個網站說明的很詳細。




相關文章:

Google Analytics