TQC-308 判斷地標區域

TQC-308 判斷地標區域

這是TQC的308題,

程式碼為自行撰寫,

僅供參考。

 

主要練習用LocationManager,

注意,地點要轉換成16進位。

 



    
    
    
    
        
            
                
                
            
        

    
    

 

 



  

 

package COM.TQC.GDD03;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import android.location.Criteria;
import android.os.Bundle;
import android.widget.TextView;

public class GDD03 extends Activity
{
  public String strLocationPrivider = "";
  public TextView mTextView01;
    
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    findview();
    
    
    setlocation();
    
  }
  
  public void findview()
  {
	  mTextView01 = (TextView)findViewById(R.id.myTextView1);
  }
  
  public void setlocation()
  {
	  LocationManager mlocManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
	  LocationListener mlocListener = new MyLocationListener();
	  mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);	    
	  getLocationPrivider();
  }
  
  public class MyLocationListener implements LocationListener
  {

	  @Override
	  public void onLocationChanged(Location loc)
	  {
		  loc.getLatitude();
		  loc.getLongitude();
		  
		  String Text;
		  //String Text = "My current location is: "+  "Latitud = " + loc.getLatitude() + "Longitud = " + loc.getLongitude();
		  //Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
		  
		  //if ((loc.getLatitude() <= 25.051578) && (loc.getLatitude() >= 25.051216) && (loc.getLongitude() >= 121.549197) && (loc.getLongitude() <= 121.549666))
		  if ((loc.getLatitude() <= 26) && (loc.getLatitude() >= 25) && (loc.getLongitude() >= 121) && (loc.getLongitude() <= 122))
		  {
			  Text = "Inside My current location is: "+  "Latitud = " + loc.getLatitude() + "  Longitud = " + loc.getLongitude();
			  Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
			 
	      }
		  
		  else
		  {
			  Text = "Outside My current location is: "+  "Latitud = " + loc.getLatitude() + "  Longitud = " + loc.getLongitude();
			  Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
			 
	      }
	  }

	  @Override
	  public void onProviderDisabled(String provider)
	  {

		  Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
	  }

	  @Override
	  public void onProviderEnabled(String provider)
	  {
		  Toast.makeText( getApplicationContext(),  "Gps Enabled", Toast.LENGTH_SHORT).show();

	  }

	  @Override
	  public void onStatusChanged(String provider, int status, Bundle extras)
	  {

	  }
  }



  public void getLocationPrivider()
  {
    try
    {
      Criteria mCriteria01 = new Criteria();
      mCriteria01.setAccuracy(Criteria.ACCURACY_FINE);
      mCriteria01.setAltitudeRequired(false);
      mCriteria01.setBearingRequired(false);
      mCriteria01.setCostAllowed(true);
      mCriteria01.setPowerRequirement(Criteria.POWER_LOW);
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
}

 

 

自我LV~