javaandroidosmdroid

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


Solution

  • 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;
           }
    
       }));