我正試圖從谷歌地圖實現MyLocationOverlay
類。但是,當我嘗試使用我自己的locationManager時 - 我無法在地圖上繪製覆蓋圖。我想知道如果我做錯了什麼。自定義我的位置覆蓋更新時間
我不想調用MyLocationOverlay類的超級方法(enbaleMyLocation),因爲該請求從locationManager方式更新得太快,並且會使我的電池活着。
這裏是我的代碼:
private class CenterOverlay extends MyLocationOverlay {
private Context mContext;
private LocationManager mLocManager;
public CenterOverlay(Context context, MapView mapView) {
super(context, mapView);
this.mContext = context;
}
@Override
public void onLocationChanged(Location location) {
super.onLocationChanged(location);
try {
doExternalCenterOverlayTask(location);
} catch (JSONException e) {
e.printStackTrace();
ErrorHandler.serviceException(mContext);
} catch (IOException e) {
e.printStackTrace();
ErrorHandler.IOException(mContext);
} catch (ServiceException e) {
e.printStackTrace();
ErrorHandler.serviceException(mContext);
}
}
@Override
public boolean enableMyLocation() {
mLocManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 50, this);
mLocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 25, this);
return mLocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
|| mLocManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
@Override
public void disableMyLocation() {
super.disableMyLocation();
mLocManager.removeUpdates(this);
}
@Override
public void onProviderDisabled(String provider) {
super.onProviderDisabled(provider);
ViewAdapter.createStandardAlertDialog(mContext,
"Your location provider has been disabled, please re-enable it.");
}
@Override
public void onProviderEnabled(String provider) {
super.onProviderEnabled(provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
super.onStatusChanged(provider, status, extras);
if (status == 0) {
ViewAdapter.showLongToast(mContext, "Location is not available at this time");
} else if (status == 1) {
//Trying to connect
} else if (status == 2) {
// Available
}
}
}
謝謝你的帖子,我似乎無法讓它工作,但它崩潰了「GeoPoint leftGeo = new GeoPoint(」。我剛創建一個新的CenterOverlay對象,在新創建的對象上調用enableMyLocation(),然後將對象添加到疊加層mapView.getOverlays()。add(obj)。我做錯了什麼?提前謝謝! – Dr1Ku 2010-11-24 00:33:01