Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the screen coordinates of a map's InfoWindow view

I have an InfoWindow in a GoogleMap with buttons in it and am trying a variant of chose007's hack to get those to work (see this question).

My approach relies on getting the screen coordinates of the button inside InfoWindow and operating on it in a onDispatchTouchEvent function.

However, all attempts to get the screen coordinates fail. I'm calling it inside onDispatchTouchEvent (i.e. long after it has been created), but I get 0,0 for both the button and the view.

I assume this is because InfoWindow's are somehow special cases and not actual UI elements? (IIRC the entire view is basically just a bitmap that's being blitted on top of the map?) Is there any way to get the coordinates? I should mention that I'm trying to avoid anything that involves magic numbers, like chose007's solution in the link.

Here's a simplified illustration of what I'm doing:

public boolean onDispatchTouchEvent(MotionEvent ev) {
    int[] location = new int[2];
    mTestButton.getLocationOnScreen(location);  // Returns 0,0
    mTestView.getLocationOnScreen(location);    // Returns 0,0

    return false;
}

public View getInfoContents(Marker marker) {
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mTestView = inflater.inflate(R.layout.info_window, null);
    mTestButton = ((Button) mTestView.findViewById(R.id.test_button));
}
like image 483
EboMike Avatar asked Nov 11 '22 07:11

EboMike


1 Answers

So you've hit a dead end on the infowindow because that's not what you should be using to make really custom infowindows that have buttons and such. Not this Yankee engineer wouldn't do it that way.

If you want something custom to show up when the user clicks the marker then capture the marker click show your view at the marker coordinates center the map to the marker coordinates and return true from the onMarkerClick

https://developers.google.com/maps/documentation/android/marker#marker_click_events

public boolean onMarkerClick (Marker marker)

Called when a marker has been clicked or tapped. Parameters marker The marker that was clicked. Returns

true if the listener has consumed the event (i.e., the default behavior should not occur), false otherwise (i.e., the default behavior should occur). The default behavior is for the camera to move to the map and an info window to appear.

So how do I put something over the map that takes the shape of a infowindow well I'd need a image or some fancy line art that's not something I have readily available in the source I've worked to cut and paste here.

So how do I put a button over the map. Well that I can do but for this example the buttons are in static locations. The Pseudo infowindow you'll have to move the button or image whatever to the coordinates of the marker and maybe center the map on the marker coordinates.

<?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="fill_parent" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="34dp" >

        <TextView
            android:id="@+id/tvSpeed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:textSize="55sp" />

        <TextView
            android:id="@+id/tvSpeedUnits"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:textSize="21sp" />
    </LinearLayout>

    <Button
        android:id="@+id/btnFollow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="34dp"
        android:background="@drawable/mapbutton"
        android:padding="14dp"
        android:text="@string/btnFollowText"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/tvSatLock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="sat lock"
        android:textColor="@color/black"
        android:textSize="21sp" />

    <Button
        android:id="@+id/btnGPS"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/mapbutton"
        android:paddingLeft="14dp"
        android:paddingRight="14dp"
        android:text="Enable GPS"
        android:textColor="@color/black" />

</RelativeLayout>

Personally I whould simply show a deluxe transparent overlay on the entire screen when the infowindow is clicked. I mean the user has to click something either way you go.

Good Luck Signed One Click ahead of the infoWindow listener.

like image 123
danny117 Avatar answered Nov 15 '22 00:11

danny117