Skip to content Skip to sidebar Skip to footer

Two Finger Rotation For Osmdroid Mapview

I'm trying to make two finger rotation for my offline maps using osmdroid. I'm new to using maps for android. I have a geoTIFF, I'm planning to extract info using NDK and later sen

Solution 1:

Have you taken a look at the OpenStreetMapViewer sample app? There is the RotationGestureOverlay overlay in there that specifically shows how to do this. It isn't as smooth as I would like it, but it will do the job.

Solution 2:

For simplicity, use the following classes:

publicclassRotationGestureDetector {

publicinterfaceRotationListener {
    publicvoidonRotate(float deltaAngle);
}

protectedfloat mRotation;
private RotationListener mListener;

publicRotationGestureDetector(RotationListener listener) {
    mListener = listener;
}

privatefloatrotation(MotionEvent event) {
    double delta_x = (event.getX(0) - event.getX(1));
    double delta_y = (event.getY(0) - event.getY(1));
    double radians = Math.atan2(delta_y, delta_x);
    return (float) Math.toDegrees(radians);
}

publicvoidonTouch(MotionEvent e) {
    if (e.getPointerCount() != 2)
        return;

    if (e.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN) {
        mRotation = rotation(e);
    }

    float rotation = rotation(e);
    float delta = rotation - mRotation;
    mRotation += delta;
    mListener.onRotate(delta);
}

}

And

publicclassRotationGestureOverlayextendsOverlayimplementsRotationGestureDetector.RotationListener
{

privatefinal RotationGestureDetector mRotationDetector;
private RotationGestureDetector.RotationListener rotationListener;


publicRotationGestureOverlay(Context context, RotationGestureDetector.RotationListener rotationListener)
{
    super(context);
    this.rotationListener = rotationListener;
    mRotationDetector = newRotationGestureDetector(this);
}

@OverridepublicbooleanonTouchEvent(MotionEvent event, MapView mapView)
{
    if (this.isEnabled()) {
        mRotationDetector.onTouch(event);
    }
    returnsuper.onTouchEvent(event, mapView);
}

@OverridepublicvoidonRotate(float deltaAngle)
{
    rotationListener.onRotate(deltaAngle);
}

@Overridepublicvoiddraw(Canvas canvas, MapView mapView, boolean b) {

}

}

Add the overlay:

mapView.getOverlays().add(new RotationGestureOverlay(context,this);

Post a Comment for "Two Finger Rotation For Osmdroid Mapview"