如何在地图上控制我的逐项覆盖?

时间:2011-09-22 07:36:01

标签: android android-mapview itemizedoverlay

我想在地图上长时间触摸时添加新的地方,我可以,但问题是当我添加叠加时我无法移动地图。代码如下。

public class CustomPinPointOverlay extends ItemizedOverlay<OverlayItem> {

    private GeoPoint newMarkerLocation;

    public CustomPinPointOverlay(GeoPoint newMarkerLocation,
            Drawable defaultMarker) {

        super(boundCenterBottom(defaultMarker));
        this.newMarkerLocation = newMarkerLocation;
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return new OverlayItem(this.newMarkerLocation, null, null);
    }

    public void draw(android.graphics.Canvas canvas, MapView mapView,
            boolean shadow) {
        super.draw(canvas, mapView, false);
    }

    @Override
    public int size() {
        return 1;
    }
}

这是我的overlay类,它扩展了itemizedoverlay。代码的其他部分是;

public PMapView(Context context, AttributeSet attrs) {
    super(context, attrs);

  setOnTouchListener(new OnTouchListener() {

        int x, y;
        long start, stop;


        @Override
        public boolean onTouch(View v, MotionEvent e) {

            if (e.getAction() == MotionEvent.ACTION_DOWN) {

                start = e.getEventTime();
                x = (int) e.getX();
                y = (int) e.getY();

                touchedPoint = map.getProjection().fromPixels(x, y);
            }
            if (e.getAction() == MotionEvent.ACTION_UP) {
                stop = e.getEventTime();

            }

            if (stop - start > 1000) {

                setEnabled(true);
                Drawable myMarker = getResources().getDrawable(
                        R.drawable.yeni_pin);
                getOverlays().add(
                        new CustomPinPointOverlay(touchedPoint, myMarker));


                return false;
            }


            return true;
        }


    });

}

这段代码有什么问题?

1 个答案:

答案 0 :(得分:1)

这个答案假设您的PMapView扩展了MapView。

如果上述陈述属实,您应该尝试拨打超类onTouchEvent,除非您处理它。例如(这是您的代码修改但我没有测试过它)

@Override
    public boolean onTouch(View v, MotionEvent e) {

        boolean retValue = true;
        if (e.getAction() == MotionEvent.ACTION_DOWN) {

            start = e.getEventTime();
            x = (int) e.getX();
            y = (int) e.getY();

            touchedPoint = map.getProjection().fromPixels(x, y);
        }
        if (e.getAction() == MotionEvent.ACTION_UP) {
            stop = e.getEventTime();

        }

        if (stop - start > 1000) {

            setEnabled(true);
            Drawable myMarker = getResources().getDrawable(
                    R.drawable.yeni_pin);
            getOverlays().add(
                    new CustomPinPointOverlay(touchedPoint, myMarker));


            retValue = false;
        }

        if(!retValue)
            super.onTouchEvent(e);
        return retValue;
    }

上面的代码会将未捕获的操作(捕获长按)传递回父级,以便处理移动地图等操作。此代码尚未经过测试,因此可能会产生奇怪的结果。您可能希望删除if(!retValue),并始终致电super.onTouchEvent(e)