當前位置:編程學習大全網 - 源碼下載 - android如何獲取地理位置

android如何獲取地理位置

三種方式進行定位,獲取用戶位置,分別是基於基站定位, 網絡定位,GPS定位。

1.基站定位(passive):這是基於網絡基站進行定位的,定位的精確度在幾十米到幾千米不等,在城市中基站覆蓋率比較高,推薦使用基站定位,如果是在郊區,基站相距較遠,基站的覆蓋沒有城裏好,定位的誤差比較大。如果在郊區不推薦使用基站定位。

2.網絡定位:wifi定位,網絡定位

3.GPS定位:與衛星進行通信。手機中嵌入了GPS模塊(精簡版的A-GPS),通過A-GPS搜索衛星, 獲取經緯度。使用GPS的弊端是:必須站在空曠的地方,頭頂對著天空,如果雲層厚了,也會受到壹定的影響。精確度:10-50米

擴展知識:

使用Android是定位必備的權限:

< uses-permission android:name= " android.permission.ACCESS_FINE_LOCATION " /> //精確定位

<uses-permission android:name= "android.permission.ACCESS_MOCK_LOCATION" /> //模擬器

<uses-permission android:name= "android.permission.ACCESS_COARSE_LOCATION" /> //粗糙定位

//獲取定位管理對象

LocationManager lm=(LocationManager)getSystemService(LOCATION_SERVICE);

String[] names=lm.getAllProviders();//獲取所有的位置提供者,壹般三種

Criteria criteria=new Criteria();//查詢條件,如果設置了海拔,則定位方式只能是GPS;

criteria.setCostAllowed(true);//是否產生開銷,比如流量費

String provider=lm.getBaseProvider(criteria,true)//獲取最好的位置提供者,第二個參數為true,表示只獲取那些被打開的位置提供者

lm.requestLocationUpdates(provier,0,0,new LocationListener(){});//獲取位置。第二個參數表示每隔多少時間返回壹次數據,第三個參數表示被定位的物體移動每次多少米返回壹次數據。

private class MyLocationListener implements LocationListener {

@Override

public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override

public void onProviderEnabled(String provider) {

}

@Override

@Override

public void onLocationChanged(Location location) {

System. out.println( "服務中位置監聽發送了變化了" );

float accuracy = location.getAccuracy(); // 精確度

double altitude = location.getAltitude(); // 海拔

double latitude = location.getLatitude(); // 緯度

double longitude = location.getLongitude(); // 經度

String locationInfo = "jingdu:" + longitude + ",weidu:" + latitude + ",haiba:" + altitude + ",jingquedu:" + accuracy;

Editor edit = sp.edit();

edit.putString( "location", locationInfo);

edit.commit();

}

} public void onProviderDisabled(String provider) {

}

  • 上一篇:大數據分析 幫妳把繁雜工作變簡單
  • 下一篇:歐睿宇邦櫥櫃好不好歐睿宇邦櫥櫃報價
  • copyright 2024編程學習大全網