Android 成功 使用GPS獲取當前地理位置(解決getLastKnownLocation 返回 null)
阿新 • • 發佈:2019-02-13
@Override
public void onCreate(final Bundle icicle) {
super.onCreate(icicle);
this.setContentView(R.layout.activity_mapview);
mBtnDone =(Button) findViewById(R.id.btn_done);
mBtnDone.setOnClickListener(this);
mapView = (MapView) findViewById(R.id.map_view);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mDefaultMarker = getResources().getDrawable(R.drawable.map_redpin);
mDefaultMarker.setBounds(0, 0, mDefaultMarker.getIntrinsicWidth(),
mDefaultMarker.getIntrinsicHeight());
mBuoyOverlay = new BuoyItemizedOverlay(mDefaultMarker, this);
initDensityDpi();
mZoomLevel = mapView.getMaxZoomLevel() - 1;
/ / LocationListener 最好在Activity的onCreate()方法中進行例項化,當GPS獲得Location時,會自 動呼叫onLocationChanged方法.
mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(final Location loc) {
LogHelper.i(TAG, "onLocationChanged. loc: " + loc);
if (loc != null) {
LogHelper.i(TAG, "onLocationChanged. latitude: "
+ loc.getLatitude() + " , longtitude: ".getLongitude());
GeoPoint geoPoint = MapUtils.getGeoPoint(loc);
mapController.animateTo(geoPoint);
initBuoyOverlayItems(loc);
} else {
Toast( MapViewActivity.this, "Your current location is temporarily unavailable.",
Toast.LENGTH_SHORT).show();
}
}
// 當系統Setting -> Location & Security -> Use wireless networks取消勾選,Use GPS satellites取消勾選時呼叫
public void onProviderDisabled(final String s) {
LogHelper.i(TAG, "onProviderDisabled. ");
}
// 當系統Setting -> Location & Security -> Use wireless networks勾選,Use GPS satellites勾 選時呼叫
public void onProviderEnabled(final String s) {
LogHelper.i(TAG, "onProviderEnabled. ");
}
public void onStatusChanged(final String s, final int i, final Bundle b) {
LogHelper.i(TAG, "onStatusChanged. ");
}
};
}
@Override
public void onStart() {
super.onStart();
mapController.setZoom(mZoomLevel);
if (!DoSomeGoodUtils.isNetworkAvailable(this)) {
mBtnDone.setEnabled(false);
showDialog(DIALOG_NO_NETWORK);
} else {
// 判斷Use GPS satellites.是否勾選
boolean isGpsEnabled = MapUtils.isGPSProviderAvaliable(this);
// 判斷Use wireless networks 是否勾選
boolean isWIFIEnabled = MapUtils.isWIFIProviderAvaliable(this);
if (!isGpsEnabled && !isWIFIEnabled) {
如果都沒有勾選,則彈出對話方塊,提示使用者勾選。
}
else {
Location lastKnownLocation = null;
// 如果只是Use GPS satellites勾選,即指允許使用GPS定位
if (isGpsEnabled && !isWIFIEnabled) { lastKnownLocation=mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
mProviderName = LocationManager.GPS_PROVIDER;
// 如果只是Use wireless networks勾選,即只允許使用網路定位。
} else if(!isGpsEnabled && isWIFIEnabled){
lastKnownLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
mProviderName = LocationManager.NETWORK_PROVIDER;
// 如果二者都勾選,優先使用GPS,因為GPS定位更精確。
} else if (isGpsEnabled && isWIFIEnabled) { lastKnownLocation=mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
mProviderName = LocationManager.GPS_PROVIDER;
if (lastKnownLocation == null) {
lastKnownLocation =mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
mProviderName = LocationManager.NETWORK_PROVIDER;
}
}
if (!TextUtils.isEmpty(mProviderName)) {
mLocationManager.requestLocationUpdates(
mProviderName, 1000, 1, mLocationListener);
}
// 如果一下子就能定位成功,則執行以下程式碼,當用網路定位時,大都能一次性定位成功,當用GPS時,該程式碼不會起太大作用。
if (lastKnownLocation != null) {
mBtnDone.setEnabled(true);
// 獲取當前地理位置
GeoPoint lastKnownPoint = getLastKnownPoint(lastKnownLocation);
// 以動畫方式移動到該地理位置
mapController.animateTo(lastKnownPoint);
// 更新浮標。該方法在這裡就不公開了。知道它的含義就行
initBuoyOverlayItems(lastKnownLocation);
}
}
}
}
@Override
protected void onResume() {
super.onResume();
LogHelper.i(TAG, "onResume. Provider Name: " + mProviderName);
if (!TextUtils.isEmpty(mProviderName)) {
// 當GPS定位時,在這裡註冊requestLocationUpdates監聽就非常重要而且必要。
沒有這句話,定位不能成功。
mLocationManager.requestLocationUpdates(mProviderName, 1000, 1,
mLocationListener);
}
}
@Override
protected void onPause() {
super.onPause();
// 取消註冊監聽
if (mLocationManager != null) {
mLocationManager.removeUpdates(mLocationListener);
}
}
}
對於定位方式:有些同行,更傾向於使用getBestProvider方法,但是我認為這種方式有他的弊端, 不是所有的手機都支援 “使用getBestProvider獲取最適合的Location” ,最好就是使用網路定位和GPS定位....
注: MapUtils是我自己寫的一個工具類