Two finger rotation for OSMdroid mapview

半城伤御伤魂 提交于 2019-12-24 13:44:08

问题


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 send it to JAVA. I need to use the geoPoint to align with True North Up using compass as well. How can I proceed, any help?

I did try this: Android Two finger rotation example, but no luck, my app was not able to detect any two finger event.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
    setContentView(R.layout.activity_main);
    mapView = (MapView)findViewById(R.id.mapview);

    //Mapview touch utilities
    mapView.setClickable(true);
    mapView.setBuiltInZoomControls(true);
    mapView.setMultiTouchControls(true);

    //set initial zoom-level, depends on your need
    mapView.getController().setZoom(nMyZoom); 

    //Display Position Overlay
    /* Itemized Overlay */
    {
            /* Create a static ItemizedOverlay showing a some Markers on some cities. */
            final ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
            items.add(new OverlayItem("Fruchthalle", "Kaiserslautern", new GeoPoint(nMyLat, nMyLong)));

            /* OnTapListener for the Markers, shows a simple Toast. */
            this.mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
                            new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
                                    @Override
                                    public boolean onItemSingleTapUp(final int index, final OverlayItem item) {
                                            Toast.makeText(
                                                            MainActivity.this,
                                                            "Place '" + item.getTitle()+ "' (index=" + index
                                                                            + ") got single tapped up", Toast.LENGTH_LONG).show();
                                            return true; // We 'handled' this event.
                                    }

                                    @Override
                                    public boolean onItemLongPress(final int index, final OverlayItem item) {
                                            Toast.makeText(
                                                            MainActivity.this,
                                                            "Place '" + item.getTitle() + "' (index=" + index
                                                                            + ") got long pressed", Toast.LENGTH_LONG).show();
                                            return false;
                                    }
                            }, mResourceProxy);
            this.mapView.getOverlays().add(this.mMyLocationOverlay);
    }

回答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.




回答2:


For simplicity, use the following classes:

public class RotationGestureDetector {

public interface RotationListener {
    public void onRotate(float deltaAngle);
}

protected float mRotation;
private RotationListener mListener;

public RotationGestureDetector(RotationListener listener) {
    mListener = listener;
}

private float rotation(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);
}

public void onTouch(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

public class RotationGestureOverlay extends Overlay implements RotationGestureDetector.RotationListener
{

private final RotationGestureDetector mRotationDetector;
private RotationGestureDetector.RotationListener rotationListener;


public RotationGestureOverlay(Context context, RotationGestureDetector.RotationListener rotationListener)
{
    super(context);
    this.rotationListener = rotationListener;
    mRotationDetector = new RotationGestureDetector(this);
}

@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
    if (this.isEnabled()) {
        mRotationDetector.onTouch(event);
    }
    return super.onTouchEvent(event, mapView);
}

@Override
public void onRotate(float deltaAngle)
{
    rotationListener.onRotate(deltaAngle);
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean b) {

}

}

Add the overlay:

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


来源:https://stackoverflow.com/questions/21226036/two-finger-rotation-for-osmdroid-mapview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!