package wyf.wpf;			//宣告套件名稱

import java.util.List;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;


import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.app.Activity;
import android.location.LocationListener;
import android.location.LocationProvider;
import android.util.Log;
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 Sample_15_2 extends MapActivity {	//繼承MapActivity的子類別
	LocationManager mLocationManager;//LocationGPS主程式，實作一個LocationManager物件mLocationManager
	MapView mv;
	MapController controller;	//宣告MapController物件
	Bitmap bmpArrow;			//宣告Bitmap物件
	RadioButton rbNormal;		//宣告RadioButton物件
	RadioButton rbSatellite;	//宣告RadioButton物件
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
        bmpArrow = BitmapFactory.decodeResource(getResources(), R.drawable.arrow);
		mv = (MapView)findViewById(R.id.mv);			//取得MapView物件
		  controller = mv.getController();				//取得MapController物件
		  mv.setBuiltInZoomControls(true);				//設置是否顯示放大縮小按鈕
		  rbNormal = (RadioButton)findViewById(R.id.normal);	//取得RadioButton物件
  	      rbSatellite = (RadioButton)findViewById(R.id.satellite);	//取得RadioButton物件	
		  RadioGroup rg = (RadioGroup)findViewById(R.id.rg);	//取得RadioGroup物件		
		  rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if(checkedId == rbNormal.getId()){		//判斷按下的是否是正常視圖
					mv.setSatellite(false);
					mv.setTraffic(true);
				}
				else if(checkedId == rbSatellite.getId()){	//判斷按下的是否為衛星視圖
					mv.setSatellite(true);
					mv.setStreetView(true);
				}
			}
		});
    	
    }
    
	
   
    //在Resume階段設定mLocationListener介面，可以獲得地理位置的更新資料     
    protected void onResume() {        
    	if (mLocationManager != null) {            
    		mLocationManager.requestLocationUpdates(                
    				LocationManager.GPS_PROVIDER,                
    				0,                
    				0,                
    				mLocationListener);        
    		}                
    	super.onResume();    
    }
    //實作mLocationListener介面
    public LocationListener mLocationListener = new LocationListener() 
    { 
    	//GPS位置資訊被更新
    	public void onLocationChanged(Location location) {        
    		TextView mTextView1 = (TextView)findViewById(R.id.textView1);
    		TextView mTextView2 = (TextView)findViewById(R.id.textView2);
    		mTextView1.setText("緯度-Latitude：  " + String.valueOf(location.getLatitude()));
    		mTextView2.setText("經度-Longitude：  " + String.valueOf(location.getLongitude()));
    		String sLong = mTextView1.toString().trim();	//取得輸入的經度
    		String sLat = mTextView2.toString().trim();	//取得輸入的緯度
    		
    		double dLong =location.getLongitude();
    		double dLat = location.getLatitude();
    		updateMapView(dLong, dLat);
    		
    		
    		}
    	public void onProviderDisabled(String provider) {    
        	
    	}     
    	public void onProviderEnabled(String provider) {    
    	
    	}  
    	//GPS位置資訊的狀態被更新
    	public void onStatusChanged(String provider, int status, Bundle extras) {        
    		switch (status) {        
    			case LocationProvider.AVAILABLE:            
    				Log.v("Status", "AVAILABLE");            
    				break;        
    			case LocationProvider.OUT_OF_SERVICE:            
    				Log.v("Status", "OUT_OF_SERVICE");            
    				break;        
    			case LocationProvider.TEMPORARILY_UNAVAILABLE:            
    				Log.v("Status", "TEMPORARILY_UNAVAILABLE");            
    				break;        
    				}
    	    }
    };
        
    	     
	protected boolean isRouteDisplayed() {	//重寫isRouteDisplayed方法
		return false;
	}
	public void updateMapView(double dLat,double dLong){
		GeoPoint gp = new GeoPoint((int)(dLat*1E6), (int)(dLong*1E6));
		mv.displayZoomControls(true);		//設定顯示放大縮小按鈕
		controller.animateTo(gp);			//將地圖移動到指定的地理位置
		List<Overlay> ol = mv.getOverlays();	//獲得MapView的Overlay
		ol.clear();
		ol.add(new ArrowOverLay(gp,bmpArrow));	//新增一個新的Overlay
		
	 }
   
}