GPS定位能提供精確, 詳細的數據。但是有的時候我們不能通過GPS獲得數據,如在屋子里面,無GPS功能等情況。那我們就需要其他的定位手段,基站定位是一個不錯的選擇。
當我們手機開機時,手機會自動向信號最強的無線通訊台聯系,注冊信息,這個通訊台就是我們所說的基站,每個基站都有自己的id,我們通過這個基站的id能夠找到基站的位置,而國內城市的基站密度可以達到500米以下或者更低,所以能夠大體上確定我們的位置。
准備工具:
1. TelephonyManager: 主要提供了一系列用於訪問與手機通訊相關的狀態和信息的get方法。其中包括手機SIM的狀態和信息、電信網絡的狀態及手機用戶的信息。在這里我們就是通過這個類獲得基站信息。
2. GsmCellLocation:裝載着從TelephonyManager中獲得的信息。
3. JSONObject,JSONArray:組建json相關的類。
4. 聯網相關的類。
代碼:
1.啟動按鈕和畫板
mTextView = (TextView) findViewById(R.id.textview);
mButton = (Button) findViewById(R.id.button);
2.獲得基站信息
mTManager = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation gcl = (GsmCellLocation) mTManager.getCellLocation();
int cid = gcl.getCid();
int lac = gcl.getLac();
int mcc = Integer.valueOf(mTManager.getNetworkOperator().substring(0,
3));
int mnc = Integer.valueOf(mTManager.getNetworkOperator().substring(3,
5));
String getNumber = "";
getNumber += ("cid:"+cid + "\n");
getNumber += ("cid:"+lac + "\n");
getNumber += ("cid:"+mcc + "\n");
getNumber += ("cid:"+mnc + "\n");
3.創建json
try {
JSONObject jObject = new JSONObject();
jObject.put("version", "1.1.0");
jObject.put("host", "maps.google.com");
jObject.put("request_address", true);
if (mcc == 460) {
jObject.put("address_language", "zh_CN");
} else {
jObject.put("address_language", "en_US");
}
JSONArray jArray = new JSONArray();
JSONObject jData = new JSONObject();
jData.put("cell_id", cid);
jData.put("location_area_code", lac);
jData.put("mobile_country_code", mcc);
jData.put("mobile_network_code", mnc);
jArray.put(jData);
jObject.put("cell_towers", jArray);
4. 創建連接,發送請求並接受回應
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(
"http://www.google.com/loc/json");
StringEntity se = new StringEntity(jObject.toString());
post.setEntity(se);
HttpResponse resp = client.execute(post);
BufferedReader br = null;
if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
br = new BufferedReader(
new InputStreamReader(resp.getEntity().getContent()));
StringBuffer sb = new StringBuffer();
}
5. 獲得數據 參見json Server Response
StringBuffer sb = new StringBuffer();
String result = br.readLine();
while (result != null) {
sb.append(getNumber);
sb.append(result);
result = br.readLine();
}
mTextView.setText(sb.toString());
能力:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
以下是GPS定位方式
獲取位置信息分為三步:
1. 添加系統權限,來支持對LBS硬件的訪問
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">
</uses-permission>
2. 得到系統服務 的LocationManager 對象
LocationManager loctionManager;
String contextService=Context.LOCATION_SERVICE;
//通過系統服務,取得LocationManager對象
loctionManager=(LocationManager) getSystemService(contextService);
3. 得到位置提供器,通過位置提供器,得到位置信息,可以指定具體的位置提供器,也可以提供一個標准集合,讓系統根據 標准匹配最適合的位置提供器,位置信息是由位置提供其提供的。
a. 通過GPS位置提供器獲得位置(指定具體的位置提供器)
String provider=LocationManager.GPS_PROVIDER;
Location location = loctionManager.getLastKnownLocation(provider);
b. 使用標准集合,讓系統自動選擇可用的最佳位置提供器,提供位置
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);//高精度
criteria.setAltitudeRequired(false);//不要求海拔
criteria.setBearingRequired(false);//不要求方位
criteria.setCostAllowed(true);//允許有花費
criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗
//從可用的位置提供器中,匹配以上標准的最佳提供器
String provider = loctionManager.getBestProvider(criteria, true);
//獲得最后一次變化的位置
Location location = loctionManager.getLastKnownLocation(provider);
最后將位置信息顯示在TextView中,如圖:
監聽位置的變化
//監聽位置變化,2秒一次,距離10米以上
loctionManager.requestLocationUpdates(provider, 2000, 10, locationListener);
//位置監聽器
private final LocationListener locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
//當位置變化時觸發
@Override
public void onLocationChanged(Location location) {
//使用新的location更新TextView顯示
updateWithNewLocation(location);
}
};
通過改變位置經緯度,程序會自動更新TextView顯示的位置信息