android+-+MapView地图里标记自己的地点,生成新的地点

本文介绍了一种在地图应用中通过长按时标记位置的方法。使用自定义的Touchy类来响应触摸事件,并通过CustomPinpoint类在地图上放置标记。当用户长按屏幕超过一定时间后,系统会询问用户是否将该位置设为当前位置或目的地。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
	 * This is class for touch event ,when user touched the screen for 
	 * enough time, user can tag the touched place as user's favorite
	 * place, or fix current position and set as destination.
	 * @author xinyan
	 *@date 2011-10-10
	 */
	class Touchy extends Overlay {

		@Override
		public boolean onTouchEvent(MotionEvent e, MapView mapView) {
			Log.v(TAG, "Touchy is touched...");
			if (MotionEvent.ACTION_DOWN == e.getAction()) {
				start = e.getEventTime();
				x = (int) e.getX();
				y = (int) e.getY();
				touchPoint = mMapView.getProjection().fromPixels(x, y);
				Log.v(TAG, "Touchy is touched.. and we get touch point.");
			}
			if (MotionEvent.ACTION_UP == e.getAction()) {
				stop = e.getEventTime();
			}
			if (stop - start > 1500) {
				OverlayItem overlayItem = new OverlayItem(touchPoint,
						"Pined position", "A new position");
				CustomPinpoint custom = new CustomPinpoint(marker,
						StandardActivity.this);
				custom.insertPinpoint(overlayItem);
				mMapView.getOverlays().add(custom);

				new AlertDialog.Builder(StandardActivity.this)
						.setIcon(null)
						.setTitle(R.string.whatYouWant)
						.setSingleChoiceItems(
								R.array.select_dialog_whatYouWant, 0,
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int whichButton) {

										if (0 == whichButton) {

											// user clicked fix my current
											// position choice
										} else if (1 == whichButton) {

											// user clicked set as destination
											// choice
										}
									}
								})
						.setPositiveButton(R.string.positive,
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int whichButton) {

										if (0 == whichButton) {

											// user clicked fix my current
											// position choice
										} else if (1 == whichButton) {

											// user clicked set as destination
											// choice

										} else if (3 == whichButton) {

											// user clicked put into favorite
											// choice
										}
									}
								})
						.setNegativeButton(R.string.cancel,
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int whichButton) {

										dialog.dismiss();
									}
								}).create().show();
			} // end of if (stop - start > 1500)
			else {
				// I think this overrided method shouldn't capture all
				// of the touch events, otherwise we can't control the map.
				return false;
			}
			return true;
		}

	}


/**
 * This is a custom class of pin point overlay,
 * it means this overlay appears a marker when you click a place on 
 * the mapview. The StandardActivtiy will use it. 
 * @author xinyan
 *@date 2011-10-9
 *
 */
public class CustomPinpoint extends ItemizedOverlay<OverlayItem> {
	private final ArrayList<OverlayItem> pinpoints = new ArrayList<OverlayItem>();
	private Context c;

	public CustomPinpoint(Drawable defaultMarker) {
		super(defaultMarker);
		// TODO Auto-generated constructor stub
	}

	public CustomPinpoint(Drawable marker, Context context) {
		this(marker);
		c = context;
	}

	@Override
	protected OverlayItem createItem(int i) {
		return pinpoints.get(i);
	}

	@Override
	public int size() {
		return pinpoints.size();
	}
	
	public void insertPinpoint(OverlayItem item) {
		pinpoints.add(item);
		this.populate();
	}

}

说明下这里所说的标记地点是当你长按地图某个地方的时候,就生成一个新的气泡标记它。

关键的地方就是Touchy这个Overlay子类,它实现了Touch事件的响应方法。我们只要在onCreate方法里面把Touchy的对象,也就是实现了Touch事件的响应方法的Overlay这一层加进MapView里的Overlays里面去,就可以实现事件的响应了、、当事件响应后我们就可以标记气泡,然后取得地址、想干嘛、干嘛


貌似还有更简单的方法、、那就是CustomPinpoint覆盖ItemizedOverlay里面的public boolean onTap(GeoPoint p, MapView mapView) 、、、没试过、、如果是这样就可以不用Touchy这个类了
















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值