问题描述
我试图建立一个监听器ondrag当了谷歌地图的片段,但不能得到拖拽事件触发。由于地图不直接支持拖放事件,我想实现一拖侦听查看。我了解onMarkerDrag事件,但确实我没有好,因为我不是要四处移动的标记。
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:
首先,以检测用户是否已平移的地图。如果他们没有,我想保持在屏幕上的位置,和动画自己的位置,如果它不是在地图上。如果他们平移地图,我认为他们知道他们想看到和在屏幕上强制位置。 (这似乎喜欢的事,应该包含在API中)
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)
第二个原因诱捕是让用户绘制线条和多边形在地图上。我能做到这一点与事件的onTap(加分),但我希望能够让他们写意它。
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.
我的code是这样的:
My code looks like this:
mainActivity:
mainActivity:
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
setUpMapIfNeeded();
mMapView = findViewById(R.id.map);
myMapDragListener = new MapDragListener();
mMapView.setOnDragListener(myMapDragListener);
监听器:
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;
}
}
地图工作正常。监听器实例,但ondrag当我的事件永远不会触发。任何想法?
The map works fine. The listener instantiates, but my onDrag event never fires. Any ideas?
推荐答案
1)创建包装类:
public class MapWrapperLayout extends FrameLayout {
public interface OnDragListener {
public void onDrag(MotionEvent motionEvent);
}
private OnDragListener mOnDragListener;
public MapWrapperLayout(Context context) {
super(context);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (mOnDragListener != null) {
mOnDragListener.onDrag(ev);
}
return super.dispatchTouchEvent(ev);
}
public void setOnDragListener(OnDragListener mOnDragListener) {
this.mOnDragListener = mOnDragListener;
}
}
2)创建MapFragment类的子类:
2) Create subclass of MapFragment class:
public class CustomMapFragment extends SupportMapFragment {
private View mOriginalView;
private MapWrapperLayout mMapWrapperLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mOriginalView = super.onCreateView(inflater, container, savedInstanceState);
mMapWrapperLayout = new MapWrapperLayout(getActivity());
mMapWrapperLayout.addView(mOriginalView);
return mMapWrapperLayout;
}
@Override
public View getView() {
return mOriginalView;
}
public void setOnDragListener(MapWrapperLayout.OnDragListener onDragListener) {
mMapWrapperLayout.setOnDragListener(onDragListener);
}
3.1)最后,在你的活动:
3.1) Finally, in your activity:
// Google map init block
CustomMapFragment customMapFragment = ((CustomMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
customMapFragment.setOnDragListener(new MapWrapperLayout.OnDragListener() {
@Override
public void onDrag(MotionEvent motionEvent) {
Log.d("ON_DRAG", String.format("ME: %s", motionEvent));
// Handle motion event:
}
});
GoogleMap map = customMapFragment.getMap();
3.2)...在你的布局:
3.2) ... and in your layout:
<fragment android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="your.package.CustomMapFragment"/>
这篇关于创建OnDragListener的谷歌地图V2片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!