如何在SupportMapFragment中使用OSMDroid在地图上启用LongClick

时间:2013-12-16 11:50:01

标签: android osmdroid supportmapfragment long-press

我有一个带有OSMDroid地图的SupportMapFragment。如何通过点击地图来实现LongClick / LongPress,以及如何进入此纬度/长度?

我的代码。我有一个Eclipse错误(在buttom代码中)。

'public class MapFragment扩展了SupportMapFragment         实现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 个答案:

答案 0 :(得分:1)

实施如下:

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();