Creating OnDragListener for Google Map v2 Fragment

Tim picture Tim · Jun 20, 2013 · Viewed 21.1k times · Source

I’m trying to setup an onDrag Listener for a google map fragment, but can’t get the drag event to fire. Since the map doesn't support drag events directly, I'm trying to implement a drag listener for the View. I know about the onMarkerDrag event, but that does me no good since I'm not trying to move markers around.

The reason for trapping map drag events is twofold:

First to detect if the user has panned the map. If they haven't, I like to keep their location on the screen, and animate to their location if it's not on the map. If they have panned the map, I assume they know what they want to see and force the location on the screen. (this seems like something that should be included in the API)

The second reason for trapping is to allow users to draw lines and polygons on the map. I can do this with onTap events(adding points), but I'd like to be able to let them freehand it as well.

My code looks like this:

mainActivity:

    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    setUpMapIfNeeded();
    mMapView = findViewById(R.id.map);
    myMapDragListener = new MapDragListener();
    mMapView.setOnDragListener(myMapDragListener);

The Listener:

public class MapDragListener implements OnDragListener {
    /**
     * 
     */
    public MapDragListener() {
        super();
        say.logCat("MapDragListener: I'm ALIVE!!!!");
    }

    private AkamaiAnnounce say = new AkamaiAnnounce();
    private boolean inDrag;
    private boolean hasPanned=false;

    @Override
    public boolean onDrag(View v, DragEvent event) {
        say.logCat("MapDragListener.onDrag: " + event.toString());
        //String dotTAG = (String) getTag();
        if (event.getLocalState() != this) {
          return false;
        }
        boolean myResult = true;
        int action = event.getAction();
        float x = event.getX();
        float y = event.getY();
        switch (action) {
        case DragEvent.ACTION_DRAG_STARTED:
          inDrag = true;
          break;
        case DragEvent.ACTION_DRAG_LOCATION:
          break;
        case DragEvent.ACTION_DRAG_ENTERED:
          break;
        case DragEvent.ACTION_DRAG_EXITED:
          break;
        case DragEvent.ACTION_DROP:
          myResult = false;
          break;
        case DragEvent.ACTION_DRAG_ENDED:
          inDrag = false; // change color of original dot back
          hasPanned=true;
          break;
        default:
          myResult = false;
          break;
        }
        return false;
        //return myResult;

    }

    public boolean hasPanned() {
        return hasPanned;
    }

    public void setHasPanned(boolean hasPanned) {
        this.hasPanned = hasPanned;
    }

}

The map works fine. The listener instantiates, but my onDrag event never fires. Any ideas?

Answer

Yyy picture Yyy · Jun 26, 2017

To get drag event listener a fragment containing googleMap, one can use below method of google. We can get the target position of map.

googleMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
          @Override
          public void onCameraIdle() {   
              Log.e(TAG,"==camera idle=="+ googleMap.getCameraPosition().target);

          }
      });
googleMap.setOnCameraMoveStartedListener(new GoogleMap.OnCameraMoveStartedListener() {
        @Override
        public void onCameraMoveStarted(int reason) {
            if (reason ==REASON_GESTURE) {
                isMaptouched=true;
                Toast.makeText(getActivity(), "The user gestured on the map.",
                        Toast.LENGTH_SHORT).show();
            } else if (reason ==REASON_API_ANIMATION) {
                Toast.makeText(getActivity(), "The user tapped something on the map.",
                        Toast.LENGTH_SHORT).show();
            } else if (reason ==REASON_DEVELOPER_ANIMATION) {
                Toast.makeText(getActivity(), "The app moved the camera.",
                        Toast.LENGTH_SHORT).show();
            }
        }


    });