PATRIK27 / osmdroid

Automatically exported from code.google.com/p/osmdroid
0 stars 0 forks source link

Not able to Drag and Drop OverlayItems on Map using osmdroid 3.0.4 #225

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Using osmdroid-3.0.1, I have implemented putting OverlayItems and Dragging it 
to different places on map, with help of ItemizedOverlay.

But now in 3.0.4, the API has been changed, so when I am trying to implement in 
same way, I am facing lot many problems.

1. Not able to Drag
2. Not able to get Old Items with different marker icon

Using new API 3.0.4, It seems I have to rebuild the whole App, which is not a 
good idea.

Please suggest me for best way.

Original issue reported on code.google.com by kish.i...@gmail.com on 31 May 2011 at 9:01

GoogleCodeExporter commented 8 years ago
How did you implement it before, and what features are missing from the latest 
build to accomplish your goal?

Original comment by kurtzm...@gmail.com on 31 May 2011 at 6:09

GoogleCodeExporter commented 8 years ago
I'm also trying to make Overlay items draggable. I used this sample code which 
works fine with the Google Maps API but doesn't seem to work with the OSMDroid 
jar:

https://github.com/commonsguy/cw-advandroid/blob/master/Maps/NooYawkTouch/src/co
m/commonsware/android/maps/NooYawk.java

I also tried to use the lastDrawMatrix solution in Issue 11, but I just can't 
get the hitTest to pass. If anyone could help me out with some sample code, I'd 
really appreciate it.

Original comment by march...@gmail.com on 31 May 2011 at 6:22

GoogleCodeExporter commented 8 years ago
Regarding HitTest: Issue 11 is quite old and probably not valid anymore (we 
should close that!). Our HitTest works exactly like Google Maps' HitTest. From 
the API: "The hit point is relative to the marker's bounds. The default 
implementation just checks to see if the hit point is within the touchable 
bounds of the marker."

We should have some examples in the code on how to use HitTest. It requires a 
little work on your end setting things up.

Regarding drag-and-drop - this would be a neat feature and something we may 
want to consider as a packaged overlay available to all users.

Original comment by kurtzm...@gmail.com on 31 May 2011 at 6:36

GoogleCodeExporter commented 8 years ago
Thanks! I got drag-and-drop OverlayItems working now with 
osmdroid-android-3.0.4.jar using the example I linked above and the code 
changes below. Hope it helps someone.

It would be awesome to have it included as a packaged overlay, although it 
requires a ImageView in your layout file as in the example.

Looks like HitTest wasn't passing because the pixel to coordinate conversion 
stuff is a bit different than GoogleMaps. Thanks for the hint regarding an 
example for HitTest.

@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();
    final Projection pj = mapView.getProjection();

    boolean result = false;

    if (action == MotionEvent.ACTION_DOWN) {
        for (OverlayItem item : items) {

            pj.fromMapPixels(x, y, t);
            pj.toPixels(item.getPoint(), p);

            if (hitTest(item, marker, t.x - p.x, t.y - p.y)) {
                result = true;
                inDrag = item;
                items.remove(inDrag);
                populate();

                xDragTouchOffset = 0;
                yDragTouchOffset = 0;

                setDragImagePosition(x, y);
                dragImage.setVisibility(View.VISIBLE);

                xDragTouchOffset = t.x - p.x;
                yDragTouchOffset = t.y - p.y;

                break;
            }
        }
    } else if (action == MotionEvent.ACTION_MOVE && inDrag != null) {
        dragImage.setVisibility(View.VISIBLE);
        setDragImagePosition(x, y);
        result = true;
    } else if (action == MotionEvent.ACTION_UP && inDrag != null) {
        dragImage.setVisibility(View.GONE);

        GeoPoint pt = pj.fromPixels(x - xDragTouchOffset, y - yDragTouchOffset);
        OverlayItem toDrop = new OverlayItem(inDrag.getTitle(),
                inDrag.getSnippet(), pt);

        items.add(toDrop);
        populate();
        inDrag = null;
        result = true;

        pj.fromMapPixels(x, y, t);

        if((t.x - p.x) == xDragTouchOffset && (t.y - p.y) == yDragTouchOffset){
            Log.d(TAG, "Do something here if desired because we didn't move item " + toDrop.getTitle());
        }
    }

    return (result || super.onTouchEvent(event, mapView));
}

Original comment by march...@gmail.com on 2 Jun 2011 at 6:22

GoogleCodeExporter commented 8 years ago
Hi,

Can anyone elaborate on this? I am getting this error:

"The method onTouchEvent(MotionEvent, MapView) of type 
OpenstreetmapdroidActivity must override a superclass method."

Where should I include this method? On the main activity or within the 
ItemizedOverlay class which is within another .java?

Finally, when I copied the code it started complaning abou "t" and other stuff?

Thanks. 

Original comment by kako1...@gmail.com on 31 Oct 2011 at 7:30

GoogleCodeExporter commented 8 years ago
Ok, I figure out that on JAVA 1.5 you cannot use @Override on interfaces but 
only on super class.  However, even when that is true, still onTouchEvent is 
not getting fired when I touch the overlay item.  I had also tried OnTap 
without any luck:

protected boolean onTap(int index) {
      //OverlayItem item = mOverlays.get(index);
      System.out.println("onTap!");
      return true;
    }

Furthremore, onTouch is either getting called:

    m_mapView.setOnTouchListener(new View.OnTouchListener() {

           public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            System.out.println("onTouch!");
            return false;
           }

The only one that seems to be working for me is dispatchTouchEvent which cannot 
be used:

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        System.out.println("dTouchEvent!"); 
        return true; 
    }

Thanks. 

Original comment by kako1...@gmail.com on 1 Nov 2011 at 3:47

GoogleCodeExporter commented 8 years ago
onTouchEvent is getting called which is a progress. Nonetheless, when it get to 
hitTest my application is crashing, I believe the problem is defaultMaker?

Thanks. 

public boolean onTouchEvent(MotionEvent event, MapView mapView) {

        final int action = event.getAction();
        final int x = (int) event.getX();
        final int y = (int) event.getY();

        final Projection pj = mapView.getProjection();

        boolean result = false;
        //Object TAG;
        //Log.d(TAG, "onTouchEvent entered");

        System.out.println("onTouchEvent!");        
        Point p = new Point(0,0);
        Point t = new Point(0,0);

        //System.out.print(MotionEvent.ACTION_DOWN);
        //System.out.print(action);

        if (action == MotionEvent.ACTION_DOWN) {

            System.out.println("Action Down!");

            for (OverlayItem item : mOverlays) {

                // Create a new GeoPoint from pixel coordinates (x, y, pointReuse):
                //pj.fromMapPixels(x, y, t);

                // Convert the given GeoPoint to onscreen pixel coordinates (GeoPoint, pointOut):
                pj.toPixels(item.getPoint(), p);

                //System.out.println(t.x);
                //System.out.println(p.x);

                //System.out.println(t.y);
                //System.out.println(p.y);

                defaultMarker = item.getDrawable();

                if (hitTest(item, defaultMarker, x - p.x, y - p.y)) {
                    System.out.println("Action Down -> IF!");
                    result = true;
                    inDrag = item;
                    mOverlays.remove(inDrag);
                    populate();

                    xDragTouchOffset = 0;
                    yDragTouchOffset = 0;

                    setDragImagePosition(x, y);
                    dragImage.setVisibility(View.VISIBLE);

                    xDragTouchOffset = t.x - p.x;
                    yDragTouchOffset = t.y - p.y;

                    break;
                }
            }

        } 

        else if (action == MotionEvent.ACTION_MOVE && inDrag != null) {
            //dragImage.setVisibility(View.VISIBLE);
            setDragImagePosition(x, y);
            System.out.println("Action Move!");
            result = true;
        } 

        else if (action == MotionEvent.ACTION_UP && inDrag != null) {
            dragImage.setVisibility(View.GONE);

            GeoPoint pt = (GeoPoint) pj.fromPixels(x - xDragTouchOffset, y - yDragTouchOffset);
            OverlayItem toDrop = new OverlayItem(inDrag.getTitle(),
                    inDrag.getSnippet(), pt);

            mOverlays.add(toDrop);
            populate();

            inDrag = null;
            result = true; 

            pj.fromMapPixels(x, y, t);

            if((t.x - p.x) == xDragTouchOffset && (t.y - p.y) == yDragTouchOffset) {
                System.out.println ("Do something here if desired because we didn't move item " + toDrop.getTitle() );
            }

            System.out.println("Action Up!");

        }
        System.out.print(inDrag);
        return (result || super.onTouchEvent(event, mapView));
    }

Original comment by kako1...@gmail.com on 3 Nov 2011 at 3:21

GoogleCodeExporter commented 8 years ago

Original comment by kurtzm...@gmail.com on 12 Apr 2013 at 3:33