Skip to content

Instantly share code, notes, and snippets.

@vishalpatel1327
Last active February 24, 2017 11:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vishalpatel1327/ccc3073e1b483b22510ec80bbb58bc91 to your computer and use it in GitHub Desktop.
Save vishalpatel1327/ccc3073e1b483b22510ec80bbb58bc91 to your computer and use it in GitHub Desktop.
Sticky Infowindows When you click on your marker, this all stuff because our map doent gives any touchlistner,clicklistner etc.
public class customeMapview extends MapView {
MapsFragment.OnCustomEventListener mListener;
private boolean ismoved = false;
public customeMapview(Context context) {
super(context);
}
public customeMapview(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
public customeMapview(Context context, AttributeSet attributeSet, int i) {
super(context, attributeSet, i);
}
public customeMapview(Context context, GoogleMapOptions googleMapOptions) {
super(context, googleMapOptions);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
/**
*onIterceptTouchListner Handle two things
*1) when map move it stop listner
*2) when touch on map but not swipe map like tap on marker tap on plan area at that time this call
**/
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
//when map move stop to give data to listner
if (ev.getAction() == MotionEvent.ACTION_MOVE) {
ismoved = true;
}
if (ev.getAction() == MotionEvent.ACTION_UP) {
// if map not moved just tap give data to listner
if (!ismoved) {
if (mListener != null)
mListener.onEvent(ev);
}
ismoved = false;
}
return super.onInterceptTouchEvent(ev);
}
public void setCustomEventListener(MapsFragment.OnCustomEventListener eventListener) {
mListener = eventListener;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.mypkg.customeMapview
android:id="@+id/mapView"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- yeh this is my personal sticky infoWindow screen-->
<FrameLayout
android:id="@+id/fram_info_content"
android:layout_width="match_parent"
android:clickable="true"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/mapView" />
</RelativeLayout>
.............
.........
// this TestFragment fragment content info windows UI
private TestFragment testFragment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.maps_fragment, container, false);
mMapView = (customeMapview) rootView.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.setCustomEventListener(new OnCustomEventListener() {
@Override
public void onEvent(MotionEvent event) {
if (testFragment != null) {
// I only care if the event is an DOWN action
if (testFragment.isVisible()) {
// create a rect for storing the fragment window rect
Rect r = new Rect(0, 0, 0, 0);
// retrieve the fragment's windows rect
testFragment.getView().getHitRect(r);
// check if the event position is inside the window rect
boolean intersects = r.contains((int) event.getX(), (int) event.getY());
// if the event is not inside then we can close the fragment
//if intersects= true it means u click out of the fragment(infowindow)
if (intersects) {
FragmentTransaction fragmentTransaction;
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.remove(testFragment).commit();
// notify that we consumed this event
}
}
}
}
});
}
............
...........
//lets setup our Custom + static pos info window screen
@Override
public boolean onMarkerClick(Marker marker) {
testFragment = new TestFragment(getActivity(), marker);
android.support.v4.app.FragmentTransaction ft1 = getFragmentManager().beginTransaction();
ft1.replace(R.id.fram_info_content, testFragment);
ft1.commit();
return true;
}
........
............
//custome listner to fetch data from Custom map view because map does't allow anu touch/click listner
public interface OnCustomEventListener {
void onEvent(MotionEvent ev);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment