简体   繁体   中英

How to set Drag Start and End listener on my custom osmdroid map?

I need to display a toast for Drag Events in my map, the below is my code in which i get only "Drag Started" message when i do a drag end-event. How can i make it?

public void onDragListener() {
    osm.setMapListener(new DelayedMapListener(new MapListener() {

        @Override
        public boolean onScroll(ScrollEvent paramScrollEvent) {
            // public boolean onDrag(boolean b) {
            int drag = DragEvent.ACTION_DRAG_STARTED;
            if (drag == 1) {
                Toast.makeText(getBaseContext(), "Drag Started",
                        Toast.LENGTH_LONG).show();

            }
            int drag1 = DragEvent.ACTION_DRAG_ENDED;
            if (drag1 == 1) {
                Toast.makeText(getBaseContext(), "Drag Stopped",
                        Toast.LENGTH_LONG).show();
            }

            return true;
        }

    }));
}

Any help will be appreciated, Thanks

Basically the logic is incorrect it will only show Drag Started Toast. This is how you can fix the toast.

Define variable at class level.

    private Context ctx;

then assign the value

  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.select_location);


    ctx = getApplicationContext();

then use it in your code

map.setMapListener(new DelayedMapListener(new MapListener() {

        @Override
        public boolean onScroll(ScrollEvent paramScrollEvent) {
            // public boolean onDrag(boolean b) {
            IGeoPoint ij = map.getMapCenter();
            Double lat = ij.getLatitude();
            Double lon = ij.getLongitude();

           Toast.makeText(ctx, lat+","+lon, Toast.LENGTH_SHORT).show();


            return true;
        }


       @Override
       public boolean onZoom(ZoomEvent event) {
           return false;
       }

   }));

you can't fire scroll start/end or zoom up/down directly... using 'DelayedMapListener' into setMapListener to fire event with delay. like this:

    map.setMapListener(new DelayedMapListener(new MapListener() {
        @Override
        public boolean onScroll(ScrollEvent event) {
            //do something
            return true;
        }
        @Override
        public boolean onZoom(ZoomEvent event) {
            //do something
            return false;
        }
    }, 100));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM