安卓实现定位功能的4种方式
android 定位一般有四種方法,這四種方式分別是:GPS定位,WIFI定準(zhǔn),基站定位,AGPS定位,
?????????????????????????????
(1)Android GPS:需要GPS硬件支持,直接和衛(wèi)星交互來獲取當(dāng)前經(jīng)緯度,這種方式需要手機支持GPS模塊(現(xiàn)在大部分的智能機應(yīng)該都有了)。通過GPS方式準(zhǔn)確度是最高的,但是它的缺點也非常明顯:1,比較耗電;2,絕大部分用戶默認(rèn)不開啟GPS模塊;3,從GPS模塊啟動到獲取第一次定位數(shù)據(jù),可能需要比較長的時間;4,室內(nèi)幾乎無法使用。這其中,缺點2,3都是比較致命的。需要指出的是,GPS走的是衛(wèi)星通信的通道,在沒有網(wǎng)絡(luò)連接的情況下也能用。
?????????????????????????????
要實用Adnroid平臺的GPS設(shè)備,首先需要添加上權(quán)限,所以需要添加如下權(quán)限:??
?????????????????????????????
| 1 | uses-permission android:name= android.permission.ACCESS_FINE_LOCATION? /uses-permission |
具體實現(xiàn)代碼如下:
首先判斷GPS模塊是否存在或者是開啟:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 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); //此為設(shè)置完成后返回到獲取界面 ??????????} |
如果開啟正常,則會直接進入到顯示頁面,如果開啟不正常,則會進行到GPS設(shè)置頁面:
????????????????????????????
獲取代碼如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | private voidgetLocation() ??????????{ ????????????// 獲取位置管理服務(wù) ????????????LocationManager locationManager; ????????????String serviceName = Context.LOCATION_SERVICE; ????????????locationManager = (LocationManager)this.getSystemService(serviceName); ????????????// 查找到服務(wù)信息 ????????????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); ????????????// 設(shè)置監(jiān)聽*器,自動更新的最小時間為間隔N秒(1秒為1*1000,這樣寫主要為了方便)或最小位移變化超過N米 ????????????locationManager.requestLocationUpdates(provider,100 * 1000, 500, ????????????????locationListener);? } |
?到這里就可以獲取到地理位置信息了,但是還是要顯示出來,那么就用下面的方法進行顯示:
???????????????????????????
代碼
| 1 2 3 4 5 6 7 8 9 10 11 | 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定位其實不難。基站定位一般有幾種,第一種是利用手機附近的三個基站進行三角定位,由于每個基站的位置是固定的,利用電磁波在這三個基站間中轉(zhuǎn)所需要時間來算出手機所在的坐標(biāo);第二種則是利用獲取最近的基站的信息,其中包括基站 id,location area code、mobile country code、mobile network code和信號強度,將這些數(shù)據(jù)發(fā)送到google的定位web服務(wù)里,就能拿到當(dāng)前所在的位置信息,誤差一般在幾十米到幾百米之內(nèi)。其中信號強度這個數(shù)據(jù)很重要,
???????????????????????????
? ? ? ? ? ? ? ?直接給出一個文章,這個文章寫的非常好,
???????????????????????????
?????????????? http://www.jb51.net/article/34522.htm
?????????????????????????????
(3)Android Wifi定位:根據(jù)一個固定的WifiMAC地址,通過收集到的該Wifi熱點的位置,然后訪問網(wǎng)絡(luò)上的定位服務(wù)以獲得經(jīng)緯度坐標(biāo)。因為它和基站定位其實都需要使用網(wǎng)絡(luò),所以在Android也統(tǒng)稱為Network方式。
???????????????
代碼:
?????????????????????????????
???????????????
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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服務(wù)器,代碼如下
???????????????????????????
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | 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定位與基站定位的結(jié)合,筆者也在網(wǎng)上找到一個很好的文章,筆者對此就不做任何解釋,直接給出網(wǎng)址:
??????????????????????????
?????????????? http://www.jb51.net/article/52673.htm
????????????????????????????
4. AGPS定位
AGPS(AssistedGPS:輔助全球衛(wèi)星定位系統(tǒng))是結(jié)合GSM或GPRS與傳統(tǒng)衛(wèi)星定位,利用基地臺代送輔助衛(wèi)星信息,以縮減GPS芯片獲取衛(wèi)星信號的延遲時間,受遮蓋的室內(nèi)也能借基地臺訊號彌補,減輕GPS芯片對衛(wèi)星的依賴度。和純GPS、基地臺三角定位比較,AGPS能提供范圍更廣、更省電、速度更快的定位服務(wù),理想誤差范圍在10公尺以內(nèi),日本和美國都已經(jīng)成熟運用AGPS于LBS服務(wù)(Location Based Service,基于位置的服務(wù))。AGPS技術(shù)是一種結(jié)合了網(wǎng)絡(luò)基站信息和GPS信息對移動臺進行定位的技術(shù),可以在GSM/GPRS、WCDMA和CDMA2000網(wǎng)絡(luò)中使進行用。該技術(shù)需要在手機內(nèi)增加GPS接收機模塊,并改造手機的天線,同時要在移動網(wǎng)絡(luò)上加建位置服務(wù)器、差分GPS基準(zhǔn)站等設(shè)備。AGPS解決方案的優(yōu)勢主要體現(xiàn)在其定位精度上,在室外等空曠地區(qū),其精度在正常的GPS工作環(huán)境下,可以達到10米左右,堪稱目前定位精度最高的一種定位技術(shù)。該技術(shù)的另一優(yōu)點為:首次捕獲GPS信號的時間一般僅需幾秒,不像GPS的首次捕獲時間可能要2~3分鐘
總結(jié)
以上是生活随笔為你收集整理的安卓实现定位功能的4种方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浅谈BroadcastReceiver
- 下一篇: JAVa面向对象--反射