最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android 定位的4種方式介紹

 更新時(shí)間:2014年07月24日 11:37:50   投稿:hebedich  
開發(fā)中對(duì)于地圖及地理位置的定位是我們經(jīng)常要用地,地圖功能的使用使得我們應(yīng)用功能更加完善,下面總結(jié)了一下網(wǎng)絡(luò)中現(xiàn)有對(duì)于介紹android定位的4種方式,希望對(duì)大家有幫助:

android 定位一般有四種方法,這四種方式分別是:GPS定位,WIFI定準(zhǔn),基站定位,AGPS定位,
                             
(1)Android GPS:需要GPS硬件支持,直接和衛(wèi)星交互來獲取當(dāng)前經(jīng)緯度,這種方式需要手機(jī)支持GPS模塊(現(xiàn)在大部分的智能機(jī)應(yīng)該都有了)。通過GPS方式準(zhǔn)確度是最高的,但是它的缺點(diǎn)也非常明顯:1,比較耗電;2,絕大部分用戶默認(rèn)不開啟GPS模塊;3,從GPS模塊啟動(dòng)到獲取第一次定位數(shù)據(jù),可能需要比較長的時(shí)間;4,室內(nèi)幾乎無法使用。這其中,缺點(diǎn)2,3都是比較致命的。需要指出的是,GPS走的是衛(wèi)星通信的通道,在沒有網(wǎng)絡(luò)連接的情況下也能用。
                             
要實(shí)用Adnroid平臺(tái)的GPS設(shè)備,首先需要添加上權(quán)限,所以需要添加如下權(quán)限:  
                             

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

具體實(shí)現(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); //此為設(shè)置完成后返回到獲取界面
          }

如果開啟正常,則會(huì)直接進(jìn)入到顯示頁面,如果開啟不正常,則會(huì)進(jìn)行到GPS設(shè)置頁面:
                            
獲取代碼如下:

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)聽*器,自動(dòng)更新的最小時(shí)間為間隔N秒(1秒為1*1000,這樣寫主要為了方便)或最小位移變化超過N米
            locationManager.requestLocationUpdates(provider,100 * 1000, 500,
                locationListener);  }

 到這里就可以獲取到地理位置信息了,但是還是要顯示出來,那么就用下面的方法進(jìn)行顯示:
                           
代碼

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定位的原理,自己實(shí)現(xiàn)基站/WIFI定位其實(shí)不難?;径ㄎ灰话阌袔追N,第一種是利用手機(jī)附近的三個(gè)基站進(jìn)行三角定位,由于每個(gè)基站的位置是固定的,利用電磁波在這三個(gè)基站間中轉(zhuǎn)所需要時(shí)間來算出手機(jī)所在的坐標(biāo);第二種則是利用獲取最近的基站的信息,其中包括基站 id,location area code、mobile country code、mobile network code和信號(hào)強(qiáng)度,將這些數(shù)據(jù)發(fā)送到google的定位web服務(wù)里,就能拿到當(dāng)前所在的位置信息,誤差一般在幾十米到幾百米之內(nèi)。其中信號(hào)強(qiáng)度這個(gè)數(shù)據(jù)很重要,
                           
               這里筆者就不多做解釋了,直接給出一個(gè)文章,這個(gè)文章寫的非常好,
                           
               http://m.fzitv.net/article/34522.htm
                             
(3)Android Wifi定位:根據(jù)一個(gè)固定的WifiMAC地址,通過收集到的該Wifi熱點(diǎn)的位置,然后訪問網(wǎng)絡(luò)上的定位服務(wù)以獲得經(jīng)緯度坐標(biāo)。因?yàn)樗突径ㄎ黄鋵?shí)都需要使用網(wǎng)絡(luò),所以在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服務(wù)器,代碼如下
                           
               

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)上找到一個(gè)很好的文章,筆者對(duì)此就不做任何解釋,直接給出網(wǎng)址:
                          
               http://m.fzitv.net/article/52673.htm
                            

4. AGPS定位

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

相關(guān)文章

最新評(píng)論

德保县| 香港 | 阳朔县| 南和县| 建昌县| 栖霞市| 吴忠市| 台湾省| 扬中市| 博乐市| 泸水县| 靖西县| 类乌齐县| 额尔古纳市| 西乡县| 南投县| 平乡县| 遂川县| 泸水县| 青海省| 南陵县| 海兴县| 资中县| 崇左市| 太原市| 平南县| 云林县| 银川市| 元阳县| 教育| 古丈县| 英吉沙县| 东源县| 新化县| 永丰县| 泸定县| 镇远县| 南木林县| 崇州市| 易门县| 永善县|