@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最好在A(yíng)ctivity的onCreate()方法中進(jìn)行實(shí)例化,當GPS獲得Location時(shí),會(huì )自 動(dòng)調用onLocationChanged方法.
mLocationListener = new LocationListener() {
@Override
public voidonLocationChanged(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, "Yourcurrent location is temporarily unavailable.",
Toast.LENGTH_SHORT).show();
}
}
// 當系統Setting -> Location& Security -> Use wirelessnetworks取消勾選,Use GPS satellites取消勾選時(shí)調用
public void onProviderDisabled(final String s){
LogHelper.i(TAG, "onProviderDisabled. ");
}
// 當系統Setting -> Location& Security -> Use wirelessnetworks勾選,Use GPS satellites勾 選時(shí)調用
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{
// 判斷UseGPS satellites.是否勾選
booleanisGpsEnabled = MapUtils.isGPSProviderAvaliable(this);
// 判斷Usewireless networks 是否勾選
booleanisWIFIEnabled = MapUtils.isWIFIProviderAvaliable(this);
if(!isGpsEnabled && !isWIFIEnabled){
如果都沒(méi)有勾選,則彈出對話(huà)框,提示用戶(hù)勾選。
}
else {
Location lastKnownLocation =null;
// 如果只是Use GPSsatellites勾選,即指允許使用GPS定位
if(isGpsEnabled && !isWIFIEnabled){ lastKnownLocation=mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
mProviderName =LocationManager.GPS_PROVIDER;
// 如果只是Use wirelessnetworks勾選,即只允許使用網(wǎng)絡(luò )定位。
} else if(!isGpsEnabled&& isWIFIEnabled){
lastKnownLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
mProviderName =LocationManager.NETWORK_PROVIDER;
//如果二者都勾選,優(yōu)先使用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);
}
//如果一下子就能定位成功,則執行以下代碼,當用網(wǎng)絡(luò )定位時(shí),大都能一次性定位成功,當用GPS時(shí),該代碼不會(huì )起太大作用。
if (lastKnownLocation != null){
mBtnDone.setEnabled(true);
// 獲取當前地理位置
GeoPoint lastKnownPoint =getLastKnownPoint(lastKnownLocation);
// 以動(dòng)畫(huà)方式移動(dòng)到該地理位置
mapController.animateTo(lastKnownPoint);
// 更新浮標。該方法在這里就不公開(kāi)了。知道它的含義就行
initBuoyOverlayItems(lastKnownLocation);
}
}
}
}
@Override
protected void onResume() {
super.onResume();
LogHelper.i(TAG, "onResume. Provider Name: " +mProviderName);
if(!TextUtils.isEmpty(mProviderName)) {
//當GPS定位時(shí),在這里注冊requestLocationUpdates監聽(tīng)就非常重要而且必要。
沒(méi)有這句話(huà),定位不能成功。
mLocationManager.requestLocationUpdates(mProviderName,1000, 1,
mLocationListener);
}
}
@Override
protected void onPause(){
super.onPause();
//取消注冊監聽(tīng)
if(mLocationManager != null) {
mLocationManager.removeUpdates(mLocationListener);
}
}
}