MyLocationOverlay 을 사용하여 MapView에서 자신의 위치를 쉽게 보여줄 수 있다.

아래는 GPS 관련 예제 프로그램을 만들던 중, 정리한 MyLocationOverlay 사용 예제...

public class MapViewerActivity extends MapActivity {
	private static final String TAG = "MapViewerActivity";

	private MapView mMapView;
	private MapController mMapController;

	private PowerManager.WakeLock mWakeLock;

	private MyLocationOverlay mMyLocationOverlay;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map_viewer);

		mMapView = (MapView) findViewById(R.id.mapView);
		mMapView.setBuiltInZoomControls(true);
		mMapView.displayZoomControls(true);

		mMapController = mMapView.getController();
		mMapController.setZoom(12);

		PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
		mWakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK,
				"PowerManager.WakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK)");

		setMyLocationOverlay();
	}

	@Override
	protected void onResume() {
		super.onResume();

		mWakeLock.acquire();

		if (mMyLocationOverlay == null) {
			mMyLocationOverlay = new MyLocationOverlay(this, mMapView);
		}
		mMyLocationOverlay.enableMyLocation();
	}

	@Override
	protected void onPause() {
		super.onPause();

		if (mMyLocationOverlay != null) {
			mMyLocationOverlay.disableMyLocation();
		}

		mWakeLock.release();
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	private void setMyLocationOverlay() {
		mMyLocationOverlay = new MyLocationOverlay(this, mMapView);
		mMyLocationOverlay.enableMyLocation();
		// mMyLocationOverlay.enableCompass();

		mMyLocationOverlay.runOnFirstFix(new Runnable() {
			public void run() {
				mMapController.animateTo(mMyLocationOverlay.getMyLocation());
			}
		});

		mMapView.getOverlays().add(mMyLocationOverlay);
	}
}



Posted by Huikyun