設計判斷地標所屬區域 , 設計一個判讀程式 , 判斷使用者手持手機時 , 該使用者是否在小巨蛋的區域內 , 一旦手機進入或是走出小巨蛋的區域 , 即變更TextView 的提示文字 , 以下是程式部分。
package COM.TQC.GDD03;
import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class GDD03 extends Activity implements LocationListener
{
public String strLocationProvider = "";
public TextView mTextView01;
LocationManager LManager;
double latitute , longtitute;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView01 = (TextView)findViewById(R.id.myTextView1);
getLocationPrivider();
}
public void getLocationPrivider()
{
try
{
Criteria mCriteria01 = new Criteria();
mCriteria01.setAccuracy(Criteria.ACCURACY_FINE);
//定位精準標準 , Criteria.ACCURACY_FINE 為精確
mCriteria01.setAltitudeRequired(false);
//是否需要海拔高度的資訊
mCriteria01.setBearingRequired(false);
//是否需要方向資訊
mCriteria01.setCostAllowed(true);
//是否同意用到付費的GPS Provider
mCriteria01.setPowerRequirement(Criteria.POWER_LOW);
//設定定位所需要的的電力等級
LManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
strLocationProvider = LManager.getBestProvider(mCriteria01, true);
//依照我們上面所制定的要求 , 取得最佳的定位方式(定位衛星)
//由於我是使用模擬器 , 所以回傳的定位提供者是Networks
//由於我們想要的是一個GPS , 所以之後我們會使用 LocationManager.GPS_PROVIDER
}
catch(Exception e)
{
e.printStackTrace();
}
}
@Override
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
Location location = LManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//透過 getLastKnownLocation() method 可以得知上一次定位提供者所提供的Location資訊
//所以程式在一開始執行時就可以判斷上一次的座標是否落在小巨蛋內
if(location != null) getWhere(location);
LManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 100, this);
//註冊監聽事件 , 最小更新區間為5000 milisecond , 100 meter
}
@Override
protected void onPause()
{
// TODO Auto-generated method stub
super.onPause();
LManager.removeUpdates(this);
//移除更新座標的要求
}
@Override
public void onLocationChanged(Location location)
{
// TODO Auto-generated method stub
getWhere(location);
//當偵測到座標改變時會取得座標
}
private void getWhere(Location location)
{
// TODO Auto-generated method stub
latitute = location.getLatitude();
longtitute = location.getLongitude();
Log.d("Test", "getLatitude():"+latitute+" getLongitude()"+longtitute);
//以下是判斷座標是否座落在小巨蛋內的方法
if(latitute > 25.051578 || latitute < 25.051201 || longtitute>121.549666 || longtitute<121.549197)
{
//以這四個點作判斷剛好可以圍成一個長方形的區域 , 區域外的部分一定是小巨蛋的區域之外
//但是這個長方形內仍有一小部分的區域仍不屬於小巨蛋 , 剩下這一小部分區域是一直角三角形
mTextView01.setText("Outside Taipei Area");
}
else
{
//接下來我們可以使用向量外積的方式來判斷座標是否落在這直角三角形內 , CrossProducts 超過 0 代表落在直角三角形內也就是不在小巨蛋裡
double ModelLineVectorX = 25.051216 - 25.051201 , ModelLineVectorY = 121.549666 - 121.549197;
double UserLineVectorX = latitute - 25.051201 , UserLineVectorY = longtitute - 121.549197;
double CrossProducts = UserLineVectorX * ModelLineVectorY - ModelLineVectorX * UserLineVectorY;
if(CrossProducts <= 0) mTextView01.setText("Within Taipei Area");
else mTextView01.setText("Outside Taipei Area");
}
}
@Override
public void onProviderDisabled(String provider)
{
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider)
{
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
// TODO Auto-generated method stub
}
}
再來是 AndroidManifest.xml , 因為我們要存取使用者的座標還要使用網路 , 所以要記得去註冊所需要的 use-permission。
使用模擬器來發送模擬的座標
P.S. 題目中所要求的Variable和Method皆會保留 , 也會根據題目所要求的流程去實作 , 縱使題目要求繞遠路....


沒有留言:
張貼留言