我正在尝试为google地图片段设置onDrag Listener,但无法触发拖动事件。由于地图不直接支持拖动事件,我正在尝试为View实现拖动侦听器。我知道onMarkerDrag事件,但这对我没有好处,因为我不想移动标记。
捕获地图拖动事件的原因有两个:
首先检测用户是否已平移地图。如果他们没有,我喜欢将他们的位置保留在屏幕上,如果它不在地图上,则动画显示他们的位置。如果他们已经平移了地图,我会假设他们知道他们想要看到什么并强制在屏幕上显示位置。 (这似乎应该包含在API中)
陷印的第二个原因是允许用户在地图上绘制线条和多边形。我可以使用onTap事件(添加点)来做到这一点,但我也希望能够让它们自由地使用它。
我的代码如下所示:
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事件永远不会触发。有什么想法吗?
答案 0 :(得分:19)
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类的子类:
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)最后,在你的活动中:
// 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)......并在你的布局中:
<fragment android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="your.package.CustomMapFragment"/>
答案 1 :(得分:14)
要使拖动事件侦听器包含googleMap的片段,可以使用google的以下方法。我们可以得到地图的目标位置。
googleMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
@Override
public void onCameraIdle() {
Log.e(TAG,"==camera idle=="+ googleMap.getCameraPosition().target);
}
});
googleMap.setOnCameraMoveStartedListener(new GoogleMap.OnCameraMoveStartedListener() {
@Override
public void onCameraMoveStarted(int reason) {
if (reason ==REASON_GESTURE) {
isMaptouched=true;
Toast.makeText(getActivity(), "The user gestured on the map.",
Toast.LENGTH_SHORT).show();
} else if (reason ==REASON_API_ANIMATION) {
Toast.makeText(getActivity(), "The user tapped something on the map.",
Toast.LENGTH_SHORT).show();
} else if (reason ==REASON_DEVELOPER_ANIMATION) {
Toast.makeText(getActivity(), "The app moved the camera.",
Toast.LENGTH_SHORT).show();
}
}
});
答案 2 :(得分:0)
我使用BehaviorSubject做类似的事情来判断地图是否脏(由用户移动)
storeMap.setOnCameraMoveStartedListener(new GoogleMap.OnCameraMoveStartedListener() {
@Override
public void onCameraMoveStarted(int reason) {
if (reason == REASON_GESTURE) {
mapIsDirty.onNext(true);
}
}
});
storeMap.setOnCameraIdleListener(new OnCameraIdleListener() {
@Override
public void onCameraIdle() {
if(mapIsDirty.getValue()) {
// Do happy things
mapIsDirty.onNext(false);
}
}
});
答案 3 :(得分:0)
我使用onCameraMoveCancelledListener解决了它:
f, ax = plt.subplots(figsize=(12, 4))
ax.set(xscale="log")
plt.plot(x, y)
plt.grid(which="both", axis='both')
_ = plt.xticks([20,50,100,200,500,1000,2000,5000,10000,20000],
["20", "50", "100", "200", "500", "1K", "2K", "5K", "10K", "20K"])