android定位会出现的问题,Android 定位当前位置可能出现的问题
android關于LocationManager的gps定位
LocationManager提供了兩種定位當前位置的方法 GPS和NETWORK 其中gps的誤差大概50m左右 network大概500m
起初當gps打開后 我使用Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);來獲取location 可是一直為null不知道是什么原因 網上搜了很久終于找到了解決方案
android有一個Criteria類可以直接判斷當前適合用gps或者network 并且設置LocationListener監聽器實時更新location 直接上代碼
private void getLocalAddress() {
LocationManager locationManager;
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
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);
Location location = locationManager.getLastKnownLocation(provider);
//updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10,
locationListener);
updateWithNewLocation(location);
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
private void updateWithNewLocation(Location location) {
String latLongString;
TextView myLocationText;
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "緯度:" + lat + "\n經度:" + lng;
} else {
latLongString = "無法獲取地理信息";
}
Toast.makeText(SearchXiaoQuActivity.this, latLongString,
Toast.LENGTH_LONG).show();
}
原文:http://my.oschina.net/u/2444750/blog/502489
總結
以上是生活随笔為你收集整理的android定位会出现的问题,Android 定位当前位置可能出现的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 螺旋圆舞曲琪薇好感度怎么提升 琪薇属性图
- 下一篇: linux编程课后作业,Unix/Lin