1

Can someone explain more how it is implemented and how the EventBus_singleton called in it? Code looks great but I am stuck at implementation part.

Here is the Code:

  1. https://stackoverflow.com/a/32734436/8111914
  2. https://stackoverflow.com/a/33276343/8111914
  3. https://stackoverflow.com/a/31478257/8111914

0. Activity -> activity_maps.xml

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name=".CustomMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MapsActivity" />

1. Class -> TouchableWrapper

 public class TouchableWrapper extends FrameLayout {

    GestureDetectorCompat mGestureDetector;


    public TouchableWrapper(Context context) {
        super(context);
        mGestureDetector = new GestureDetectorCompat(context, mGestureListener);
    }


    private final GestureDetector.SimpleOnGestureListener mGestureListener
            = new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Log.e("GestureDetector", "Executed");
            //Notify the event bus (I am using Otto eventbus of course) that you have just received a double-tap event on the map, inside the event bus event listener
            EventBus_Singleton.getInstance().post(new EventBus_Poster("double_tapped_map"));

            return true;
        }
    };

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        mGestureDetector.onTouchEvent(ev);
        return super.onInterceptTouchEvent(ev);
    }


}

2. Class -> EventBus_Singleton

I have used Otto for event trigger.

public class EventBus_Singleton {

    private static final Bus BUS = new Bus();

    public static Bus getInstance() {
        return BUS;
    }

    public EventBus_Singleton() {
    }

    public void post(String s) {
        BUS.post(s);
    }
}

3. Class -> EventBus_Poster

public class EventBus_Poster {
    public final String string;

    EventBus_Poster(String string) {
        this.string = string;
    }
}

Now question is how I implement above code in My Map Activity while using below code:

 @Subscribe
    public void EventBus_singleton(EventBus_Poster event) {
        Log.e("EventBus_singleton", "Executed");
        if (event != null && event.string.equals("double_tapped_map")) {
            if (mapFragment != null) {
                mapFragment.getMap().getUiSettings().setZoomGesturesEnabled(false);
                mapFragment.getMap().getUiSettings().setRotateGesturesEnabled(false);

                mapFragment.getMap().setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
                    @Override
                    public void onCameraChange(CameraPosition cameraPosition) {
                        LatLng lng = cameraPosition.target;
                        Log.e("LatLng", "" + lng);
                    }
                });
                mapFragment.getMap().animateCamera(CameraUpdateFactory.zoomIn(), 400, null);
            }

        }
    }


private final ScaleGestureDetector.OnScaleGestureListener mScaleGestureListener
        = new ScaleGestureDetector.SimpleOnScaleGestureListener() {
    /**
     * This is the active focal point in terms of the viewport. Could be a local
     * variable but kept here to minimize per-frame allocations.
     */

    float startingSpan;
    float startFocusX;
    float startFocusY;

    @Override
    public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) {
        startingSpan = scaleGestureDetector.getCurrentSpan();
        startFocusX = scaleGestureDetector.getFocusX();
        startFocusY = scaleGestureDetector.getFocusY();

        return true;
    }

    @Override
    public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
        float scale = scaleGestureDetector.getCurrentSpan() / startingSpan;

        mVelocityTracker.computeCurrentVelocity(1000);

        Log.d("VELOCITY", "X vel : " + mVelocityTracker.getXVelocity());
        Log.d("VELOCITY", "Y vel : " + mVelocityTracker.getYVelocity());

        if (scale <= 1.0) {
            EventBus_Singleton.getInstance().post(new EventBus_Poster("pinched_map", "out"));
        } else {
            EventBus_Singleton.getInstance().post(new EventBus_Poster("pinched_map", "in"));
        }

        return true;
    }
};

And Kindly don't mark it as duplicate or anything because it's the genuine question asking for code of implementation for the incomplete code.

Namita
  • 49
  • 9

0 Answers0