我正在尝试在地图叠加层上放置一个标记,然后在用户选择可绘制时弹出一个标记。问题是事件似乎重叠。当我在标记上点击(ontouch MotionEvent.ACTION_UP调用)然后某个时候其他标记消失了,我也想做类似当弹出窗口可见时,没有一个标记可以被用户拖动。有没有人对如何使这些事件互相排斥?
以下是地图活动的代码:
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
final int action = event.getAction();
final int x = (int) event.getX();
final int y = (int) event.getY();
boolean result = false;
if (action == MotionEvent.ACTION_DOWN) {
for (OverlayItem item : items) {
Point p = new Point(0, 0);
map.getProjection().toPixels(item.getPoint(), p);
if (hitTest(item, marker, x - p.x, y - p.y)) {
result = true;
inDrag = item;
items.remove(inDrag);
populate();
xDragTouchOffset = 0;
yDragTouchOffset = 0;
setDragImagePosition(p.x, p.y);
dragImage.setVisibility(View.VISIBLE);
xDragTouchOffset = x - p.x;
yDragTouchOffset = y - p.y;
Log.e("touch"," out ACTION_DOWN");
// onTap(index);
break;
}
}
} else if (action == MotionEvent.ACTION_MOVE && inDrag != null) {
//
setDragImagePosition(x, y);
Log.e("touch"," out btn");
result = true;
flag = false;
} else if (action == MotionEvent.ACTION_UP && inDrag != null) {
if (mLastMotionEventAction == MotionEvent.ACTION_DOWN){
// SOME TAP ACTIONS ...
dragImage.setVisibility(View.GONE);
GeoPoint pt = map.getProjection().fromPixels(
x - xDragTouchOffset, y - yDragTouchOffset);
OverlayItem toDrop = new OverlayItem(pt, inDrag.getTitle(),
inDrag.getSnippet());
Log.e("touch"," out last" + pt.getLatitudeE6());
items.add(toDrop);
// onTap(pt, map);
// onTap();
populate();
inDrag = null;
result = true;
flag = false;
// if (result) {
//
// }
Log.e("pop up", "pop up");
return false;
} else {
return true;
}
// onTap(index);
}
mLastMotionEventAction = event.getAction();
return (result || super.onTouchEvent(event, mapView));
}
private void setDragImagePosition(int x, int y) {
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) dragImage
.getLayoutParams();
lp.setMargins(x - xDragImageOffset - xDragTouchOffset, y
- yDragImageOffset - yDragTouchOffset, 0, 0);
dragImage.setLayoutParams(lp);
}
}
我写onTap,但这种方法不起作用。没有错误。 我想处理这两种方法。
答案 0 :(得分:2)
最后我处理了onTouchEvent和onTap重叠与拖动和弹出标记。
@Override
protected boolean onTap(final int index) {
if ( isPinch ){
return false;
}else{
// getMapView().setOnTouchListener(new OnTouchListener() {
// @Override
// public boolean onTouch(View arg0, MotionEvent arg1) {
// if (!items.isEmpty()) {
// if (view != null) {
// view.setVisibility(View.GONE);
// getMapView().invalidate();
// }
// }
// getMapView().invalidate();
// return true;
// }
// });
if (view != null) {
view.setVisibility(View.GONE);
getMapView().removeView(view);
getMapView().invalidate();
flag = false;
view = null;
}
view = getLayoutInflater().inflate(R.layout.balloon_overlay, null);
LinearLayout layout = (LinearLayout) view
.findViewById(R.id.balloon_main_layout);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
view.setBackgroundResource(R.drawable.balloon_overlay_bg_selector);
ImageView image = (ImageView) view
.findViewById(R.id.balloon_disclosure);
TextView text = (TextView) view
.findViewById(R.id.balloon_item_title);
text.setText(items.get(index).getTitle());
if (items.get(index).getTitle() != null
&& items.get(index).getTitle().equals("Me") == false) {
image.setImageResource(R.drawable.mekr);
}
Projection projection = getMapView().getProjection();
Point point = new Point();
projection.toPixels(items.get(index).getPoint(), point);
int x = (int) (view.getWidth() / 2f);
int y = -bitMap.getHeight() - 3;
MapView.LayoutParams lp = new MapView.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, items.get(index)
.getPoint(), x, y,
MapView.LayoutParams.BOTTOM_CENTER);
getMapView().removeView(view);
getMapView().invalidate();
getMapView().addView(view, lp);
getMapView().invalidate();
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (!items.isEmpty()) {
if (view != null) {
view.setVisibility(View.GONE);
getMapView().invalidate();
}
}
getMapView().invalidate();
}
});
selectedIndex = index;
return true;
}
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
final int action = event.getAction();
final int x = (int) event.getX();
final int y = (int) event.getY();
boolean result = false;
if (action == MotionEvent.ACTION_DOWN) {
for (OverlayItem item : items) {
Point p = new Point(0, 0);
map.getProjection().toPixels(item.getPoint(), p);
if (hitTest(item, marker, x - p.x, y - p.y)) {
result = true;
inDrag = item;
items.remove(inDrag);
populate();
xDragTouchOffset = 0;
yDragTouchOffset = 0;
setDragImagePosition(p.x, p.y);
dragImage.setVisibility(View.VISIBLE);
xDragTouchOffset = x - p.x;
yDragTouchOffset = y - p.y;
isPinch=false;
break;
}
}
} else if (action == MotionEvent.ACTION_MOVE && inDrag != null) {
if (view != null) {
if (view.getVisibility() != 0) {
Log.e("touch", " out move");
setDragImagePosition(x, y);
result = true;
isPinch=true;
}else{
setDragImagePosition(x, y);
isPinch=false;
}
}else{
setDragImagePosition(x, y);
result = true;
isPinch=true;
}
} else if (action == MotionEvent.ACTION_UP && inDrag != null) {
dragImage.setVisibility(View.GONE);
GeoPoint pt = map.getProjection().fromPixels(
x - xDragTouchOffset, y - yDragTouchOffset);
OverlayItem toDrop = new OverlayItem(pt, inDrag.getTitle(),
inDrag.getSnippet());
Log.e("touch", " out last" + pt.getLatitudeE6());
items.add(toDrop);
populate();
inDrag = null;
result = true;
isPinch = false;
}
return (isPinch || super.onTouchEvent(event,mapView));
}
private void setDragImagePosition(int x, int y) {
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) dragImage
.getLayoutParams();
lp.setMargins(x - xDragImageOffset - xDragTouchOffset, y
- yDragImageOffset - yDragTouchOffset, 0, 0);
dragImage.setLayoutParams(lp);
}
}
答案 1 :(得分:0)
尝试以下代码:
/** * Class used to plot the start and end points on the map * */ public class MapendPointsOverlay extends ItemizedOverlay<OverlayItem> { private ArrayList<OverlayItem> m_overlays = new ArrayList<OverlayItem>(); private BalloonOverlayView m_balloonView; private OverlayItem m_overlayitem; private Context m_context; private int m_viewOffset; private MapController m_mapController; private MapView m_mapView; private int m_screenWidth; private int m_screenHeight; private Display m_screen; private boolean m_fromroutepath = false; public MapendPointsOverlay(Drawable p_drawable) { super(p_drawable); } public MapendPointsOverlay(Drawable p_defaultMarker, Context p_context, MapView p_mapview, boolean p_fromroutepath) { super(boundCenterBottom(p_defaultMarker)); m_context = p_context; m_mapView = p_mapview; m_mapController = p_mapview.getController(); m_viewOffset = 35; m_fromroutepath = p_fromroutepath; m_screen = ((WindowManager) m_context .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); m_screenWidth = m_screen.getWidth(); m_screenHeight = m_screen.getHeight(); } /** * Method that creates an OverlayItem from the list of OverlayItem at index * given by <b>p_arg0</b> */ @Override protected OverlayItem createItem(int p_arg0) { return m_overlays.get(p_arg0); } @Override public int size() { return m_overlays.size(); } @Override protected boolean onTap(int p_index) { boolean m_isRecycled; GeoPoint m_point; m_point = createItem(p_index).getPoint(); m_overlayitem = m_overlays.get(p_index); if (m_balloonView == null) { m_balloonView = new BalloonOverlayView(m_context, m_viewOffset); m_isRecycled = false; } else { m_isRecycled = true; } m_balloonView.setVisibility(View.GONE); m_balloonView.setData(m_overlayitem); m_balloonView.setVisibility(View.VISIBLE); MapView.LayoutParams m_params = new MapView.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, m_point,MapView.LayoutParams.BOTTOM_CENTER); m_params.mode = MapView.LayoutParams.MODE_MAP; List<Overlay> m_mapOverlays = m_mapView.getOverlays(); if (m_mapOverlays.size() > 1) { hideOtherBalloons(m_mapOverlays); } if (m_isRecycled) { m_balloonView.setLayoutParams(m_params); } else { m_mapView.addView(m_balloonView, m_params); } m_mapController.animateTo(m_point); m_mapController.setCenter(m_point); return true; } /** * Sets the visibility of this overlay's balloon view to GONE. */ private void hideBalloon() { if (m_balloonView != null) { m_balloonView.setVisibility(View.GONE); } } /** * Hides the balloon view for any other BalloonItemizedOverlay instances * that might be present on the MapView. * * @param p_overlays * - list of overlays (including this) on the MapView. */ private void hideOtherBalloons(List<Overlay> p_overlays) { for (Overlay m_overlay : p_overlays) { if (m_overlay instanceof MapendPointsOverlay && m_overlay != this) { ((MapendPointsOverlay) m_overlay).hideBalloon(); } } } /** * Override this method to handle a "tap" on a balloon. By default, does * nothing and returns false. * * @param p_index * - The index of the item whose balloon is tapped. * @return true if you handled the tap, otherwise false. */ protected boolean onBalloonTap(int p_index) { return false; } }
打开信息窗口的BallonOverlay类。
public class BalloonOverlayView extends FrameLayout { private LinearLayout m_layout; private TextView m_title; private TextView m_snippet; /** * Create a new BalloonOverlayView. * * @param p_context * - The activity context. * @param p_balloonBottomOffset * - The bottom padding (in pixels) to be applied when rendering this view. */ public BalloonOverlayView(Context p_context, int p_balloonBottomOffset) { super(p_context); // m_ctx = context; setPadding(10, 0, 10, p_balloonBottomOffset); m_layout = new LinearLayout(p_context); m_layout.setVisibility(VISIBLE); LayoutInflater m_inflater = (LayoutInflater) p_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View m_view = m_inflater.inflate(R.layout.balloonoverlay, m_layout); m_title = (TextView) m_view.findViewById(R.id.balloon_item_title); m_snippet = (TextView) m_view.findViewById(R.id.balloon_item_snippet); m_snippet.setVisibility(View.GONE); ImageView m_close = (ImageView) m_view.findViewById(R.id.close_img_button); m_close.setOnClickListener(new OnClickListener(){ public void onClick(View v) { m_layout.setVisibility(GONE); } }); FrameLayout.LayoutParams m_params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); m_params.gravity = Gravity.NO_GRAVITY; addView(m_layout, m_params); } }