0

I want to add marker on google map by double tap. I know about googlemap.OnMapClickListener but it adds a marker on single tap. I have found a post on handling double tap in google map but i am unable to add marker from another class.

MapFragment.java:

        @Override
        public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_map, container, false);

            mMapView = (MapView) view.findViewById(R.id.mapView);
            mMapView.onCreate(savedInstanceState);

            mMapView.onResume(); // needed to get the map to display immediately

            try {
                MapsInitializer.initialize(getActivity().getApplicationContext());
            } catch (Exception e) {
                e.printStackTrace();
            }

            mMapView.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(GoogleMap mMap) {
                    googleMap = mMap;

                    if (latitude != null && longitude != null) {
                        // For showing a move to my location button
                        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
                        {
                            return;
                        }
                        googleMap.setMyLocationEnabled(true);

                        // For dropping a marker at a point on the Map
                        final LatLng coordinates = new LatLng(latitude, longitude);

                        // For zooming automatically to the location of the marker
                        cameraPosition = new CameraPosition.Builder().target(coordinates).zoom(15).build();
                        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

                        googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
                            @Override
                            public void onMapClick(final LatLng coordinates){

                            **//adds marker on single tap & get coordinates**//

                            }
                        });

                    }

                }

            });

            return view;
        }

@Override
public boolean onDoubleTap(MotionEvent e) {

    return true; 
}

// Here will be some autogenerated methods too

Map Fragment.xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <azizbekyan.andranik.map.OnDoubleTap
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:enabled="true"
        android:clickable="true"
        android:apiKey="YOUR_API_KEY" />        
</LinearLayout>

OnDoubleTap.java:

    public class OnDoubleTap extends MapView {

  private long lastTouchTime = -1;

  public OnDoubleTap(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  @Override
  public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
      long thisTime = System.currentTimeMillis();
      if (thisTime - lastTouchTime < ViewConfiguration.getDoubleTapTimeout()) {
        //** Double tap control here **//

        lastTouchTime = -1;
      } else {
        // Too slow 
        lastTouchTime = thisTime;
      }
    }
    return super.onInterceptTouchEvent(ev);
  }
}

Now how can I add marker on google map by double tap and get the coordinates from another class? Any help would be appreciated.

Shaifu
  • 331
  • 2
  • 15

3 Answers3

0

You can acheive it using googleMap.setonMapClickListener as in

declare global variable inside fragment

private long lastTouchTime = -1;

Update your listener inside fragment to

googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
    @Override
    public void onMapClick(final LatLng coordinates){
        long thisTime = System.currentTimeMillis();
        if (thisTime - lastTouchTime < ViewConfiguration.getDoubleTapTimeout()) {
            //** Double tapped , write logic to add Map **//
            lastTouchTime = -1;
        } else {
            lastTouchTime = thisTime;
        }
    }
});
Max Base
  • 533
  • 1
  • 5
  • 12
rajan.kali
  • 10,789
  • 3
  • 22
  • 33
  • I have tried your logic. I set googleMap.setOnMapClickListener inside fragment but it detects only single tap. On double tap it just zooms map – Shaifu Nov 21 '17 at 08:46
0

Try to implement rajan ks answer and add this line to your "onMapReady" method

googleMap.getUiSettings().setZoomControlsEnabled(false);

This will disable the Google Map double tap gesture to zoom in on the map

Romainhm
  • 11
  • 1
0

To do that, you can use approach, based on this answer of community wiki: you need to intercept touches in custom TouchableWrapper view and also manage Zoom gestures enabling/disabling:

public class TouchableWrapper extends FrameLayout {

    private GoogleMap mGoogleMap = null;
    private long mLastTouchTime = -1;

    public TouchableWrapper(Context context) {
        super(context);
    }

    public void setGoogleMap(GoogleMap googleMap) {
        mGoogleMap = googleMap;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        switch (event.getAction() & MotionEvent.ACTION_MASK) {

            case MotionEvent.ACTION_DOWN:
                mGoogleMap.getUiSettings().setZoomGesturesEnabled(false);
                long thisTime = System.currentTimeMillis();
                if (thisTime - mLastTouchTime < ViewConfiguration.getDoubleTapTimeout()) {

                    if (mGoogleMap != null) {
                        mGoogleMap.addMarker(new MarkerOptions()
                                .position(mGoogleMap.getProjection().fromScreenLocation(new Point((int)event.getX(), (int)event.getY())))
                                .title("DblTapped"));
                    }
                    mLastTouchTime = -1;
                } else {
                    mLastTouchTime = thisTime;
                    mGoogleMap.getUiSettings().setZoomGesturesEnabled(true);
                }
                break;
        }
        return super.dispatchTouchEvent(event);
    }
}

than create custom MapFragment, which swaps original and touchable views:

public class MultiTouchMapFragment extends MapFragment {
    public View mOriginalContentView;
    public TouchableWrapper mTouchView;

    @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;
    }
}

and use it in MainActivity:

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

    private GoogleMap mGoogleMap;
    private MultiTouchMapFragment mMapFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mMapFragment = (MultiTouchMapFragment) getFragmentManager()
                .findFragmentById(R.id.map_fragment);
        mMapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;
        mMapFragment.mTouchView.setGoogleMap(mGoogleMap);
    }

}

where activity_mail.xml is:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="<your_package_name>.MainActivity">

    <fragment
        android:id="@+id/map_fragment"
        android:name="<your_package_name>.MultiTouchMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

That's all did the trick.

Andrii Omelchenko
  • 12,030
  • 12
  • 40
  • 70
  • I am working on the map when I double tap its keep scrolling but when I am using pinching by two fingers and zoom its scrolling disabled how can I disabled the scrolling on double I am using this [solution](https://stackoverflow.com/questions/29352051/keep-map-centered-regardless-of-where-you-pinch-zoom-on-android) – Tanveer Munir Jan 24 '19 at 14:12
  • I am using `MapInfoWindowFragment` for map – Tanveer Munir Jan 24 '19 at 14:13