How to manage overlays in MapViewCompassDemo in an

2019-05-16 21:38发布

问题:

I am using MapViewCompassDemo sample from

\add-ons\addon-google_apis-google_inc_-7\samples\MapsDemo\

for rotating map view based on user direction.This is working

perfectly if the map is without overlays.But if i add overlays to map the ontap not working exactly

on marker while rotating.can any one help me regarding this.

回答1:

in this sample it only rotate dispatchDraw method only.Along with dispatchDraw rotate

dispatchTouchEvent also.find below code to rotate toouch events also

  @Override
    public void dispatchDraw(Canvas canvas) {
        canvas.save();
        canvas.rotate(getRotation(), getWidth() / 2, getHeight() / 2);
        super.dispatchDraw(canvas);
        canvas.restore();
    }

@Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        float[] coords = new float[] {
                event.getX(), event.getY()
        };
        adjustCoords(coords, getRotation());
        MotionEvent evt = MotionEvent.obtain(event.getDownTime(), event.getEventTime(), event
                .getAction(), coords[0], coords[1], event.getPressure(), event.getSize(), event
                .getMetaState(), event.getXPrecision(), event.getYPrecision(), event.getDeviceId(),
                event.getEdgeFlags());
        return super.dispatchTouchEvent(evt);
    }
    protected void adjustCoords(float[] coords, float deg) {
        float x = coords[0];
        float y = coords[1];
        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;
        // convert to radians
        float rad = (float) ((deg * Math.PI) / 180F);
        float s = (float) Math.sin(rad);
        float c = (float) Math.cos(rad);
        // translate point back to origin:
        x -= centerX;
        y -= centerY;
        // apply rotation
        float tmpX = x * c - y * s;
        float tmpY = x * s + y * c;
        x = tmpX;
        y = tmpY;
        // translate point back:
        x += centerX;
        y += centerY;
        coords[0] = x;
        coords[1] = y;
    }

See this below link

http://evancharlton.com/thoughts/android-rotating-touch-events/