日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android 定位的几种方式介绍

發布時間:2025/7/25 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 定位的几种方式介绍 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

[地理位置] android 定位的幾種方式介紹 ?

???????????????????? 開發中對于地圖及地理位置的定位是我們經常要用地,地圖功能的使用使得我們應用功能更加完善,下面?www.androidkaifa.com?總結了一下網絡中現有對于介紹android定位的幾種方式,希望對大家有幫助: ?????????????? ????????????? ??????????????

android 定位一般有四種方法,這四種方式分別是:GPS定位,WIFI定準,基站定位,AGPS定位, ?????????????? ?????????????? ??????????????

(1) Android GPS:需要GPS硬件支持,直接和衛星交互來獲取當前經緯度,這種方式需要手機支持GPS模塊(現在大部分的智能機應該都有了)。通過GPS方式準確度是最高的,但是它的缺點也非常明顯:1,比較耗電;2,絕大部分用戶默認不開啟GPS模塊;3,從GPS模塊啟動到獲取第一次定位數據,可能需要比較長的時間;4,室內幾乎無法使用。這其中,缺點2,3都是比較致命的。需要指出的是,GPS走的是衛星通信的通道,在沒有網絡連接的情況下也能用。要實用Adnroid平臺的GPS設備,首先需要添加上權限,所以需要添加如下權限:? ?????????????? ????????????? ???????????????

uses-permission android:name= android.permission.ACCESS_FINE_LOCATION?? /uses-permission ?????????????? ????????????? ??????????????

具體實現代碼如下: ?????????????? ????????????? ?????????????? 首先判斷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); ?????????????? ????????????? ?????????????????????? // 設置監聽*器,自動更新的最小時間為間隔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經度? +longitude); ?????????????? ????????????? ?????????????????????? } else { ?????????????? ????????????? ?????????????????????????? tv1.setText( 無法獲取地理信息 ); ?????????????? ????????????? ?????????????????????? } ?????????????? ????????????? ?????????????????? } ?????????????? ?????????????? ??????????????

(2)Android 基站定位:Android 基站定位只要明白了基站/WIFI定位的原理,自己實現基站/WIFI定位其實不難。基站定位一般有幾種,第一種是利用手機附近的三個基站進行三角定位,由于每個基站的位置是固定的,利用電磁波在這三個基站間中轉所需要時間來算出手機所在的坐標;第二種則是利用獲取最近的基站的信息,其中包括基站 id,location area code、mobile country code、mobile network code和信號強度,將這些數據發送到google的定位web服務里,就能拿到當前所在的位置信息,誤差一般在幾十米到幾百米之內。其中信號強度這個數據很重要,這里筆者就不多做解釋了,直接給出一個文章,這個文章寫的非常好,http://www.cnblogs.com/rayee/archive/2012/02/02/2336101.html

?(3)Android Wifi定位:根據一個固定的WifiMAC地址,通過收集到的該Wifi熱點的位置,然后訪問網絡上的定位服務以獲得經緯度坐標。因為它和基站定位其實都需要使用網絡,所以在Android也統稱為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地址的方法,下面是把地址發送給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定位與基站定位的結合,筆者也在網上找到一個很好的文章,筆者對此就不做任何解釋,直接給出網址: ?????????????? ????????????? ???????????????http://www.cnblogs.com/coffeegg/archive/2011/10/01/2197129.html?????????????? ?????????????? ??????????????

(4)AGPS定位:AGPS(AssistedGPS:輔助全球衛星定位系統)是結合GSM或GPRS與傳統衛星定位,利用基地臺代送輔助衛星信息,以縮減GPS芯片獲取衛星信號的延遲時間,受遮蓋的室內也能借基地臺訊號彌補,減輕GPS芯片對衛星的依賴度。和純GPS、基地臺三角定位比較,AGPS能提供范圍更廣、更省電、速度更快的定位服務,理想誤差范圍在10公尺以內,日本和美國都已經成熟運用AGPS于LBS服務(Location Based Service,基于位置的服務)。AGPS技術是一種結合了網絡基站信息和GPS信息對移動臺進行定位的技術,可以在GSM/GPRS、WCDMA和CDMA2000網絡中使進行用。該技術需要在手機內增加GPS接收機模塊,并改造手機的天線,同時要在移動網絡上加建位置服務器、差分GPS基準站等設備。AGPS解決方案的優勢主要體現在其定位精度上,在室外等空曠地區,其精度在正常的GPS工作環境下,可以達到10米左右,堪稱目前定位精度最高的一種定位技術。該技術的另一優點為:首次捕獲GPS信號的時間一般僅需幾秒,不像GPS的首次捕獲時間可能要2~3分鐘 ? ?

http://www.cnblogs.com/cuihongyu3503319/p/3863867.html

?

轉載于:https://www.cnblogs.com/cmblogs/p/4353919.html

總結

以上是生活随笔為你收集整理的android 定位的几种方式介绍的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。