android 定位的4種方式介紹
android 定位一般有四種方法,這四種方式分別是:GPS定位,WIFI定準,基站定位,AGPS定位,
(1)Android GPS:需要GPS硬件支持,直接和衛(wèi)星交互來獲取當前經(jīng)緯度,這種方式需要手機支持GPS模塊(現(xiàn)在大部分的智能機應該都有了)。通過GPS方式準確度是最高的,但是它的缺點也非常明顯:1,比較耗電;2,絕大部分用戶默認不開啟GPS模塊;3,從GPS模塊啟動到獲取第一次定位數(shù)據(jù),可能需要比較長的時間;4,室內幾乎無法使用。這其中,缺點2,3都是比較致命的。需要指出的是,GPS走的是衛(wèi)星通信的通道,在沒有網(wǎng)絡連接的情況下也能用。
要實用Adnroid平臺的GPS設備,首先需要添加上權限,所以需要添加如下權限:
uses-permission android:name= android.permission.ACCESS_FINE_LOCATION /uses-permission
具體實現(xiàn)代碼如下:
首先判斷GPS模塊是否存在或者是開啟:
private voidopenGPSSettings() {
LocationManager alm = (LocationManager)this
.getSystemService(Context.LOCATION_SERVICE);
if (alm
.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, GPS模塊正常 ,Toast.LENGTH_SHORT)
.show();
return;
}
Toast.makeText(this, 請開啟GPS! ,Toast.LENGTH_SHORT).show();
Intent intent = newIntent(Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(intent,0); //此為設置完成后返回到獲取界面
}
如果開啟正常,則會直接進入到顯示頁面,如果開啟不正常,則會進行到GPS設置頁面:
獲取代碼如下:
private voidgetLocation()
{
// 獲取位置管理服務
LocationManager locationManager;
String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager)this.getSystemService(serviceName);
// 查找到服務信息
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 =locationManager.getBestProvider(criteria, true); // 獲取GPS信息
Location location =locationManager.getLastKnownLocation(provider); // 通過GPS獲取位置
updateToNewLocation(location);
// 設置監(jiān)聽*器,自動更新的最小時間為間隔N秒(1秒為1*1000,這樣寫主要為了方便)或最小位移變化超過N米
locationManager.requestLocationUpdates(provider,100 * 1000, 500,
locationListener); }
到這里就可以獲取到地理位置信息了,但是還是要顯示出來,那么就用下面的方法進行顯示:
代碼
private voidupdateToNewLocation(Location location) {
TextView tv1;
tv1 = (TextView)this.findViewById(R.id.tv1);
if (location != null) {
double latitude = location.getLatitude();
double longitude=location.getLongitude();
tv1.setText( 維度: + latitude+ \n經(jīng)度 +longitude);
} else {
tv1.setText( 無法獲取地理信息 );
}
}
(2)Android 基站定位:Android 基站定位只要明白了基站/WIFI定位的原理,自己實現(xiàn)基站/WIFI定位其實不難?;径ㄎ灰话阌袔追N,第一種是利用手機附近的三個基站進行三角定位,由于每個基站的位置是固定的,利用電磁波在這三個基站間中轉所需要時間來算出手機所在的坐標;第二種則是利用獲取最近的基站的信息,其中包括基站 id,location area code、mobile country code、mobile network code和信號強度,將這些數(shù)據(jù)發(fā)送到google的定位web服務里,就能拿到當前所在的位置信息,誤差一般在幾十米到幾百米之內。其中信號強度這個數(shù)據(jù)很重要,
這里筆者就不多做解釋了,直接給出一個文章,這個文章寫的非常好,
http://www.fzitv.net/article/34522.htm
(3)Android Wifi定位:根據(jù)一個固定的WifiMAC地址,通過收集到的該Wifi熱點的位置,然后訪問網(wǎng)絡上的定位服務以獲得經(jīng)緯度坐標。因為它和基站定位其實都需要使用網(wǎng)絡,所以在Android也統(tǒng)稱為Network方式。
代碼:
public classWiFiInfoManager implements Serializable {
private static final long serialVersionUID= -4582739827003032383L;
private Context context;
public WiFiInfoManager(Context context) {
super();
this.context = context;
}
public WifiInfo getWifiInfo() {
WifiManager manager = (WifiManager)context
.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = new WifiInfo();
info.mac =manager.getConnectionInfo().getBSSID();
Log.i( TAG , WIFI MACis: + info.mac);
return info;
}
public class WifiInfo {
public String mac;
public WifiInfo() {
super();
}
}
}
上面是取到WIFI的mac地址的方法,下面是把地址發(fā)送給google服務器,代碼如下
public staticLocation getWIFILocation(WifiInfo wifi) {
if (wifi == null) {
Log.i( TAG , wifiis null. );
return null;
}
DefaultHttpClient client = newDefaultHttpClient();
HttpPost post = new HttpPost( http://www.google.com/loc/json );
JSONObject holder = new JSONObject();
try {
holder.put( version , 1.1.0 );
holder.put( host , maps.google.com );
JSONObject data;
JSONArray array = new JSONArray();
if (wifi.mac != null wifi.mac.trim().length() 0) {
data = new JSONObject();
data.put( mac_address , wifi.mac);
data.put( signal_strength , 8);
data.put( age , 0);
array.put(data);
}
holder.put( wifi_towers ,array);
Log.i( TAG , request json: + holder.toString());
StringEntity se = newStringEntity(holder.toString());
post.setEntity(se);
HttpResponse resp =client.execute(post);
int state =resp.getStatusLine().getStatusCode();
if (state == HttpStatus.SC_OK) {
HttpEntity entity =resp.getEntity();
if (entity != null) {
BufferedReader br = newBufferedReader(
newInputStreamReader(entity.getContent()));
StringBuffer sb = newStringBuffer();
String resute = ;
while ((resute =br.readLine()) != null) {
sb.append(resute);
}
br.close();
Log.i( TAG , response json: + sb.toString());
data = newJSONObject(sb.toString());
data = (JSONObject)data.get( location );
Location loc = newLocation(
android.location.LocationManager.NETWORK_PROVIDER);
loc.setLatitude((Double)data.get( latitude ));
loc.setLongitude((Double)data.get( longitude ));
loc.setAccuracy(Float.parseFloat(data.get( accuracy )
.toString()));
loc.setTime(System.currentTimeMillis());
return loc;
} else {
return null;
}
} else {
Log.v( TAG , state + );
return null;
}
} catch (Exception e) {
Log.e( TAG ,e.getMessage());
return null;
}
}
(3.1)而WIFI定位與基站定位的結合,筆者也在網(wǎng)上找到一個很好的文章,筆者對此就不做任何解釋,直接給出網(wǎng)址:
http://www.fzitv.net/article/52673.htm
4. AGPS定位
AGPS(AssistedGPS:輔助全球衛(wèi)星定位系統(tǒng))是結合GSM或GPRS與傳統(tǒng)衛(wèi)星定位,利用基地臺代送輔助衛(wèi)星信息,以縮減GPS芯片獲取衛(wèi)星信號的延遲時間,受遮蓋的室內也能借基地臺訊號彌補,減輕GPS芯片對衛(wèi)星的依賴度。和純GPS、基地臺三角定位比較,AGPS能提供范圍更廣、更省電、速度更快的定位服務,理想誤差范圍在10公尺以內,日本和美國都已經(jīng)成熟運用AGPS于LBS服務(Location Based Service,基于位置的服務)。AGPS技術是一種結合了網(wǎng)絡基站信息和GPS信息對移動臺進行定位的技術,可以在GSM/GPRS、WCDMA和CDMA2000網(wǎng)絡中使進行用。該技術需要在手機內增加GPS接收機模塊,并改造手機的天線,同時要在移動網(wǎng)絡上加建位置服務器、差分GPS基準站等設備。AGPS解決方案的優(yōu)勢主要體現(xiàn)在其定位精度上,在室外等空曠地區(qū),其精度在正常的GPS工作環(huán)境下,可以達到10米左右,堪稱目前定位精度最高的一種定位技術。該技術的另一優(yōu)點為:首次捕獲GPS信號的時間一般僅需幾秒,不像GPS的首次捕獲時間可能要2~3分鐘
相關文章
Android使用SurfaceView實現(xiàn)飄贊動畫
這篇文章主要為大家詳細介紹了Android如何使用SurfaceView實現(xiàn)飄贊動畫,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
Android RecycleView添加head配置封裝的實例
這篇文章主要介紹了Android RecycleView添加head配置封裝的實例的相關資料,這里提供實例幫助大家實現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08
淺談Android開發(fā)中ListView控件性能的一些優(yōu)化方法
這篇文章主要介紹了Android開發(fā)中ListView控件性能的一些優(yōu)化方法,需要的朋友可以參考下2016-01-01
Flutter版本的自定義短信驗證碼實現(xiàn)示例解析
這篇文章主要介紹了Flutter版本的自定義短信驗證碼實現(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08

