在百度map上選位置,並在上面顯示覆蓋物


在百度map上選位置,並在上面顯示覆蓋物

2014-11-03 16:15 | 201人閱讀
在百度地圖上選位置,並在上面顯示覆蓋物
之前聽說百度地圖有這麼一個功能(在地圖上選取位置),一直沒時間去看看怎麼實現,剛好手頭上項目需要這個功能,下午抽個空看了一下,集成到項目當中。

先看效果圖,這兩張圖片上的紅點就是我點擊的地方。當然,我既然可以在上面加個覆蓋物,那這個點的座標肯定可以得到的。



上代碼:
/**
 * 在百度地圖上選擇點
 * @author ck
 * @since 2014年2月23日 21:33:41
 */
public class CopyOfSelectPointInMap extends Activity
{
	// MapView 是地圖主控件
	private MapView mMapView = null;

	// 用MapController完成地圖控制
	private MapController mMapController = null;

	// 地圖覆蓋物
	private MyOverlay mOverlay = null;
	// 裝覆蓋物
	private ArrayList<MyOverlay> items;

	// MKMapViewListener 用於處理地圖事件回調
	MKMapViewListener mMapListener = null;

	// 用於截獲屏座標
	MKMapTouchListener mapTouchListener = null;

	// 當前地點擊點
	private GeoPoint currentPt = null;

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

		initialize();

		initializeViews();

		initializeListeners();
	}

	private void initialize()
	{
		MyApplication app = (MyApplication) this.getApplication();
		if (app.mBMapManager == null)
		{
			app.mBMapManager = new BMapManager(getApplicationContext());
			app.mBMapManager.init(MyApplication.strKey,
					new MyApplication.MyGeneralListener());
		}

		items = new ArrayList<CopyOfSelectPointInMap.MyOverlay>();
	};

	private void initializeViews()
	{
		mMapView = (MapView) findViewById(R.id.bmapView);
		mMapController = mMapView.getController();
		mMapController.enableClick(true);
		mMapController.setZoom(15);

		// 默認跳到天安們
		double cLat = 39.945;
		double cLon = 116.404;
		GeoPoint p = new GeoPoint((int) (cLat * 1E6), (int) (cLon * 1E6));
		mMapController.setCenter(p);
	};

	private void initializeListeners()
	{
		/**
		 * 設置地圖點擊事件監聽
		 */
		mapTouchListener = new MKMapTouchListener()
		{
			@Override
			public void onMapClick(GeoPoint point)
			{
				currentPt = point;
				updateMapState();
			}

			@Override
			public void onMapDoubleClick(GeoPoint point)
			{
			}

			@Override
			public void onMapLongClick(GeoPoint point)
			{
			}
		};
		mMapView.regMapTouchListner(mapTouchListener);
	};

	// 更新地圖的狀態
	private void updateMapState()
	{
		if (mOverlay == null)
		{
			// 創建自定義overlay
			mOverlay = new MyOverlay(getResources().getDrawable(
					R.drawable.line_end), mMapView);
			OverlayItem item1 = new OverlayItem(currentPt, "覆蓋物1", "");
			mOverlay.addItem(item1);
			items.add(mOverlay);

			mMapView.getOverlays().add(mOverlay);
		} else
		{
			mOverlay.removeAll();

			OverlayItem item1 = new OverlayItem(currentPt, "覆蓋物1", "");
			mOverlay.addItem(item1);
			items.add(mOverlay);
		}

		mMapView.refresh();

	}

	/**
	 * 自定義覆蓋物
	 */
	public class MyOverlay extends ItemizedOverlay
	{
		public MyOverlay(Drawable defaultMarker, MapView mapView)
		{
			super(defaultMarker, mapView);
		}

		@Override
		public boolean onTap(int index)
		{
			return true;
		}

		@Override
		public boolean onTap(GeoPoint pt, MapView mMapView)
		{
			return false;
		}

	}

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

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

	@Override
	protected void onDestroy()
	{
		mMapView.destroy();
		super.onDestroy();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState)
	{
		super.onSaveInstanceState(outState);
		mMapView.onSaveInstanceState(outState);

	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState)
	{
		super.onRestoreInstanceState(savedInstanceState);
		mMapView.onRestoreInstanceState(savedInstanceState);
	}

}


代碼不多,但是注意的地方還是有的,比如生命週期一定要加上,每次點擊添加覆蓋物的時候記得先把之前的清掉,還有,你造嗎?每次一進入就看到天安們你TM在逗我吧!
  •        
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章