@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);
}
}
}