Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle onTouch event for map in Google Map API v2?

GoogleMap by default doesn't provide event for map drag start and drag stop. I have already reported that problem here.

I want to make custom handler that will use plain onTouch event and combine it with setOnCameraChangeListener.

However i failed to find how I can access onTouch event of GoogleMap object. It doesn't provide such callback.

I wonder how can i handle onTouch event for map in Google Map API v2?

like image 409
Alexey Zakharov Avatar asked Dec 05 '12 12:12

Alexey Zakharov


People also ask

Can you customize Google Maps API?

The Google Maps APIs now support you in creating beautiful styled maps for your Android and iOS apps as well as your website using the same JSON style object.

What is googlemap API?

Google Maps APIs are prepackaged pieces of code that let you quickly and easily include maps on your websites or in your mobile apps – and then add extra functions to your applications. They're available for Android, iOS and web browsers, and as HTTP web services that let you pass information between systems.

What is mapview?

A View which displays a map (with data obtained from the Google Maps service). When focused, it will capture keypresses and touch gestures to move the map.


1 Answers

Here is a possible workaround for determining drag start and drag end events:

You have to extend SupportMapFragment or MapFragment. In onCreateView() you have to wrap your MapView in a customized FrameLayout (in example below it is the class TouchableWrapper), in which you intercepts touch events and recognizes whether the map is tapped or not. If your onCameraChange gets called, just check whether the map view is pressed or not (in example below this is the variable mMapIsTouched).

Example code:

UPDATE 1:

  • return original created view in getView()
  • use dispatchTouchEvent() instead of onInterceptTouchEvent()

Customized FrameLayout:

private class TouchableWrapper extends FrameLayout {     @Override     public boolean dispatchTouchEvent(MotionEvent ev) {     switch (ev.getAction()) {         case MotionEvent.ACTION_DOWN:         mMapIsTouched = true;         break;      case MotionEvent.ACTION_UP:         mMapIsTouched = false;         break;         }          return super.dispatchTouchEvent(ev);     }     } 

In your customized MapFragment:

@Override public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {     mOriginalContentView = super.onCreateView(inflater, parent, savedInstanceState);      mTouchView = new TouchableWrapper(getActivity());     mTouchView.addView(mOriginalContentView);      return mTouchView; }  @Override public View getView() {     return mOriginalContentView; } 

In your camera change callback method:

private final OnCameraChangeListener mOnCameraChangeListener = new OnCameraChangeListener() {     @Override     public void onCameraChange(CameraPosition cameraPosition) {         if (!mMapIsTouched) {             refreshClustering(false);         }     } }; 
like image 108
AZ13 Avatar answered Oct 01 '22 13:10

AZ13