如何在 SupportMapFragment 中使用 OSMDroid 在地图上启用 LongClick
Posted
技术标签:
【中文标题】如何在 SupportMapFragment 中使用 OSMDroid 在地图上启用 LongClick【英文标题】:How to enable LongClick on map with OSMDroid in SupportMapFragment 【发布时间】:2013-12-16 11:50:00 【问题描述】:我有一个带有 OSMDroid 地图的 SupportMapFragment。如何通过点击地图来实现 LongClick/LongPress,以及如何到达这个纬度/经度?
这是我的代码。我有一个 Eclipse 错误(在按钮代码中)。
'公共类 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】:转实现如下:
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();
【讨论】:
以上是关于如何在 SupportMapFragment 中使用 OSMDroid 在地图上启用 LongClick的主要内容,如果未能解决你的问题,请参考以下文章
SupportMapFragment vs MapFragment性能方面
SupportMapFragment 的 findFragmentById 在 Android Studio 中返回 null