Search code examples
androidgoogle-maps-android-api-2infowindow

How can I position InfoWindows based on screen position, instead of map position?


My custom infoWindows are larger than the default. This means I have to lower the point in which the map centers upon clicking a marker, so that the infoWindow will be shown fully. The problem is, since I move the map position down by a specific amount of latitude, when the map is zoomed in close, the offset is too great. Inversely, when the I zoom far out, it doesn't move down enough. How can I position the infoWindow relative to the screen height?

Default look of screen. X is anchor of infoWindow

|-----|
|-----|
|--X--|
|-----|
|-----|

What I want: (No matter the zoom level)

|-----|
|-----|
|-----|
|--X--|
|-----|

Solution

  • To lower the point at which the map centers on a marker can be done. Sorry but you loose the default info window implementation. Don't worry your custom info window will easily port to this method. This is a very good thing to handle onMarkerClick because now your info window can handle buttons link to external content, anything the info windows do on the google maps application like a nav button can be done at onMarkerClick() with a popup window or dialog fragment.

    Follow these steps.

    1. tell the map you are handling the onMarkerClick. mMap.setOnMarkerClickListener(this);
    2. override onMarkerClick(Marker marker) to
    3. Display a popup window http://android-er.blogspot.com/2012/03/example-of-using-popupwindow.html or dialog fragment.
    4. Move the map so your marker is at the bottom of the screen. Calculating the new lat lon involves taking into account the zoom and the direction of north. This library will help. mMap.moveCamera(CameraUpdateFactory.newLatLng(markers newlat newlon));
    5. return false; from the onMarkerClick event to tell android your handling the marker.

    Note return true from onMarkerClick() for markers you want to use the default map info window.