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.