Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6973767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:09:02+00:00 2026-05-27T17:09:02+00:00

I had been trying to implement OnTouchEvent so I can react when an user

  • 0

I had been trying to implement OnTouchEvent so I can react when an user move (drag & drop) an overlayitem.

I found something similar on the Osmdroid site: http://code.google.com/p/osmdroid/issues/detail?id=225. I am using the code there as reference but still is not working for me and then my application is crashing when it get to the hitTest.

Any suggestion? 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));
}

private void setDragImagePosition(int x, int y) {
    RelativeLayout.LayoutParams lp=
            (RelativeLayout.LayoutParams)dragImage.getLayoutParams();
    lp.setMargins(x-xDragImageOffset-xDragTouchOffset,
            y-yDragImageOffset-yDragTouchOffset, 0, 0);
    dragImage.setLayoutParams(lp);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:09:03+00:00Added an answer on May 27, 2026 at 5:09 pm

    I got the above code working. I only noticed two things different between my working code and yours. One is the hitTest paramaters you are sending, try changing

    if (hitTest(item, defaultMarker, x - p.x, y - p.y)) {

    to

    if (hitTest(item, defaultMarker, t.x - p.x, t.y - p.y)) {

    The second is that you should uncomment pj.fromMapPixels(x, y, t);

    also you should probably uncomment dragImage.setVisibility(View.VISIBLE); if you want to see the image as you are dragging it

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to implement NSXMLParser and so far I've had memory issues, performance
I have had been on a roller coaster trying to get ImageMagick to work
We are trying to get quaqua out of our application. We had been using
I'm fairly new to Lua, I've been working on trying to implement Lua scripting
I've been trying to write an extend function so I can have bunch of
I'm trying to implement the danvk draggable table Javascript, and while I've been successful
I have had quite some difficulty trying to make a User Defined Function (UDF)
I have been trying for several hours to implement a GLSL replacement for glTexGen
I had been trying to do an object serialiation,deserialisation program.When i am giving the
I've been trying to implement what should be a pretty simple feature, but I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.