risbin.p
risbin.p

Reputation: 45

google map marker drag event no working in android

I am doing an android application contains multiple markers. I want to make marker long press event, I understand that MarkerDragListener can help me to do that. Here is the code I used for MarkerDragListener:

@Override
public void onMapReady(final GoogleMap googleMap) {
    Log.d(TAG, "onMapReady()");
    mGoogleMap = googleMap;
    mGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

    mGoogleMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
        @Override
        public void onMarkerDragStart(Marker arg0) {
            // TODO Auto-generated method stub
            Log.d("System out", "onMarkerDragStart..."+arg0.getPosition().latitude+"..."+arg0.getPosition().longitude);
        }

        @SuppressWarnings("unchecked")
        @Override
        public void onMarkerDragEnd(Marker arg0) {
            // TODO Auto-generated method stub
            Log.d("System out", "onMarkerDragEnd..."+arg0.getPosition().latitude+"..."+arg0.getPosition().longitude);

        }

        @Override
        public void onMarkerDrag(Marker arg0) {
            // TODO Auto-generated method stub
            Log.i("System out", "onMarkerDrag...");
        }
    });

    mGoogleMap.setOnMarkerClickListener(this);
}

But Drag event not working for me, Someone please help me with a solution.

Upvotes: 3

Views: 4019

Answers (1)

Piotr Z
Piotr Z

Reputation: 874

from the offlicial documentation:

Markers are not draggable by default. You must explicitly set the marker to be draggable either with MarkerOptions.draggable(boolean) prior to adding it to the map, or Marker.setDraggable(boolean) once it has been added to the map.

meaning you have to make the marker draggable first, like this:

static final LatLng POSITION = new LatLng(-31.90, 115.86);
Marker marker = mGoogleMAp.addMarker(new MarkerOptions()
                      .position(PERTH)
                      .draggable(true));

after that you will observe your onMarkerDragStart(...) and onMarkerDragStart(...) callbacks invoked

Upvotes: 8

Related Questions