How to enable LongClick on map with OSMDroid in SupportMapFragment

1.3k views Asked by At

i have a SupportMapFragment with OSMDroid map. How can i implement LongClick/LongPress by tapping the map, and how get to this lat/long?

its my code. I have a Eclipse error(in buttom code).

'public class MapFragment extends SupportMapFragment implements OnMapClickListener, OnMapLongClickListener, OnCameraChangeListener {

MyMapListener myMapListener;
MainActivity activity;
Marker newMarker;    
private static View view;   
private MapView mMapView;
private MapController mMapController;
@Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);           
    this.activity=(MainActivity)activity;

  }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null)
            parent.removeView(view);
    }
    try {
        view = inflater.inflate(R.layout.map_fragment,
                container, false);
    } catch (InflateException e) {
        /* map is already there, just return view as it is */
    }


    mMapView = (MapView) view.findViewById(R.id.mapview);


    mMapView.setTileSource(TileSourceFactory.MAPNIK);


    mMapView.setBuiltInZoomControls(true);

    mMapController = (MapController) mMapView.getController();
    mMapController.setZoom(16);
    org.osmdroid.util.GeoPoint gPt = new org.osmdroid.util.GeoPoint((int) (50.2937 * 1E6),
            (int) (30.6437 * 1E6));
    mMapController.setCenter(gPt);      
    return view;
}


@Override
public void onResume() {
    super.onResume();

}

private void setUpMapIfNeeded() {
    if (mMap == null) {
        mMap = ((SupportMapFragment) activity.getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        if (mMap != null) {
            setUpMap();
        }
    }
}    

@Override
public void onMapClick(LatLng point) {

}

@Override
public void onMapLongClick(LatLng point) {

    activity.ll3buttons.setVisibility(View.VISIBLE);
     newMarker = mMap.addMarker(new MarkerOptions()
    .position(point)
    .title(ArraysEnums.contoureType[activity.bc.getContoureType()])
    .snippet(ArraysEnums.coordSource[activity.bc.getCoordSource()])
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.abc_ic_menu_share_holo_dark)));
    sendPoint(point);
}

@Override
public void onCameraChange(final CameraPosition position) {

}
private void sendPoint(LatLng point){
    activity.setCustomCoordReadyToWrite(point);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {

    int actionType = ev.getAction();

    if (actionType == MotionEvent.ACTION_DOWN) {

        startTimeForLongClick=ev.getEventTime();
        xScreenCoordinateForLongClick=ev.getX();
        yScreenCoordinateForLongClick=ev.getY();

    } else if (actionType == MotionEvent.ACTION_MOVE) {

        if (ev.getPointerCount()>1) {

            startTimeForLongClick=0;                         
        } else {

            float xmove = ev.getX(); //where is their finger now?                   
            float ymove = ev.getY();

            xlow = xScreenCoordinateForLongClick - xtolerance;
            xhigh= xScreenCoordinateForLongClick + xtolerance;
            ylow = yScreenCoordinateForLongClick - ytolerance;
            yhigh= yScreenCoordinateForLongClick + ytolerance;
            if ((xmove<xlow || xmove> xhigh) || (ymove<ylow || ymove> yhigh)){

                startTimeForLongClick=0;
            }
        }

    } else if (actionType == MotionEvent.ACTION_UP) {           
        long eventTime = ev.getEventTime();
        long downTime = ev.getDownTime();            
        if (startTimeForLongClick==downTime){                 
            if ((eventTime-startTimeForLongClick)>minMillisecondThresholdForLongClick){                    
                float xup = ev.getX();                  
                float yup = ev.getY();                    
                xlow = xScreenCoordinateForLongClick - xtolerance;
                xhigh= xScreenCoordinateForLongClick + xtolerance;
                ylow = yScreenCoordinateForLongClick - ytolerance;
                yhigh= yScreenCoordinateForLongClick + ytolerance;
                if ((xup>xlow && xup<xhigh) && (yup>ylow && yup<yhigh)){                         
                    long totaltime=eventTime-startTimeForLongClick;
                    String strtotaltime=Long.toString(totaltime);                               
                    Log.d("long press detected: ", strtotaltime);                       

                }
            }
        }

    }


    return super.dispatchTouchEvent(ev);//Eclipse error==The method dispatchTouchEvent(MotionEvent) is undefined for the type SupportMapFragment
}

} '

1

There are 1 answers

0
user2853867 On

turned implemented as follows:

MapEventsReceiver mReceive = new MapEventsReceiver() {

    @Override
    public boolean singleTapUpHelper(IGeoPoint arg0) {
        Log.d("debug", "Single tap helper");
        //your onSingleTap logic here
        return false;
    }

    @Override
    public boolean longPressHelper(IGeoPoint arg0) {
        Log.d("debug", "LongPressHelper");
        //your onLongPress logic here                  
        return false;
    }
};

MapEventsOverlay OverlayEventos = new MapEventsOverlay(activity.getBaseContext(), mReceive);
mMapView.getOverlays().add(OverlayEventos);

//Refreshing the map to draw the new overlay
mMapView.invalidate();