TQC-306 Google地圖

TQC-306 Google地圖

這是TQC的306題,

程式碼為自行撰寫,

僅供參考。

 

主要練習如何申請Google地圖:

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

 

並使用在Android中,

而在地圖有許多功能,可以參考:

http://developer.android.com/guide/topics/location/index.html

注意:座標為16進位。

 



    
    
    
    
    
        
            
                
                
            
        
        

    
    

 

 

 



 

 

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 android.widget.Toast;

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.Builder;
import com.google.android.maps.GeoPoint;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.text.Editable;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;




public class GDD03 extends com.google.android.maps.MapActivity 
{
  private MapController MapController; 
  private MapView MapView;
  private Button btn_query, btn_zoomIn, btn_zoomOut; 
  private EditText EditLongitude; //經度
  private EditText EditLatitude; //緯度
  private int intZoomLevel=17;
  private Overlay Overlay1;
  
  private double dLng=121.228626;//預設經度
  private double dLat=25.0402226; //預設緯度
  

  @Override 
  protected void onCreate(Bundle icicle) 
  { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 
    
    findview();
    setmap();
    setlistener();
    refreshMapView();
    //MapView.setStreetView(true);
    //MapController.setZoom(15);
   
    
  } 
  
  public void findview()
  {
	  MapView = (MapView) findViewById(R.id.myMapView);
	  EditLongitude=(EditText)findViewById(R.id.editText1);
	  EditLatitude=(EditText)findViewById(R.id.editText2);
	  btn_query = (Button)findViewById(R.id.btn_query); 
	  btn_zoomOut = (Button)findViewById(R.id.btn_zoomOut); 
  }
  
  public void setmap()
  {
	  MapView.setBuiltInZoomControls(true);    
	  EditLongitude.setText(String.valueOf(dLng));
	  EditLatitude.setText(String.valueOf(dLat));
	  MapController = MapView.getController();
  }
  
  public void setlistener()
  {
	  btn_query.setOnClickListener(new Button.OnClickListener() 
	    { 
	      //@Override
	      public void onClick(View v) 
	      { 
	       
	        if(EditLongitude.getText().toString().equals("")||
	           EditLatitude.getText().toString().equals(""))
	        {
	          showDialog("經度或緯度填寫不正確!");
	        }
	        else
	        {
	        	dLng=Double.parseDouble(EditLongitude.getText().toString()); 
	        	dLat=Double.parseDouble(EditLatitude.getText().toString()); 
	        	
	          refreshMapView(); 
	        }
	      } 
	    }); 
	     	    
	    btn_zoomIn = (Button)findViewById(R.id.btn_zoomIn); 
	    btn_zoomIn.setOnClickListener(new Button.OnClickListener() 
	    { 
	      //@Override 
	      public void onClick(View v) 
	      { 
	    	  if(intZoomLevel!=0)
	    	  { 	
	    	  intZoomLevel=intZoomLevel+1;
	    	  }
	    	  MapController.setZoom(intZoomLevel);
	      } 
	    }); 
	     
	 
	    btn_zoomOut.setOnClickListener(new Button.OnClickListener() 
	    { 
	      //@Override
	      public void onClick(View v) 
	      { 
	    	  intZoomLevel=intZoomLevel-1;
	    	  MapController.setZoom(intZoomLevel);   
	      } 
	    });
  }
  
  public void refreshMapView() 
  { 
    
	    int i = (int)(this.dLat * 1E6);
	    int j = (int)(this.dLng * 1E6);
	    Toast popup = Toast.makeText(GDD03.this, String.valueOf(i)+" "+String.valueOf(j), Toast.LENGTH_SHORT);
        popup.show();
	    
       // dLng = 121.228626 * 1E6;
        //dLat = 25.0402226 * 1E6;
      
       GeoPoint point = new GeoPoint( (int)i,(int) j);
       OverlayItem overlayitem = new OverlayItem(point, "", "");
      
      
       MapController.setCenter(point);
       MapController.setZoom(intZoomLevel);
	    
  } 
   
  @Override 
  protected boolean isRouteDisplayed() 
  { 
    return 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();
  }
} 

 

 

自我LV~