Search code examples
androidosmdroid

Adjust map to contain marker info window


I'm using osmdroid and osmbonuspack libraries. In activity I have a map with several markers from osmbonuspack each of them opens an InfoWindow when tapped. But when the marker is close to the top edge of the MapView its infowindow opens beyond the bounds. Is there a way to adjust mapview in order for marker's InfoWindow to be fully presented similar to this iOS library? https://www.mapbox.com/ios-sdk/examples/marker/


Solution

  • I managed to solve this problem by overriding standard MarkerInfoWindow onOpen method:

    @Override
    public void onOpen(Object item) {
        super.onOpen(item);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                int[] infoWindowTop = {0, 0};
                int[] mapViewTop = {0, 0};
                mView.getLocationOnScreen(infoWindowTop);
                mMapView.getLocationOnScreen(mapViewTop);
                int rightEnd = infoWindowTop[0] + mView.getWidth();
                int dy = infoWindowTop[1] - mapViewTop[1];
                int dx = infoWindowTop[0];
                int dx_right = mMapView.getWidth() - rightEnd;
                if (dy < 0 && dx < 0) {
                    smoothScrollBy(mMapView, dx, dy);
                    return;
                }
                if (dy < 0 && dx_right < 0) {
                    smoothScrollBy(mMapView, -dx_right, dy);
                    return;
                }
                if (dx < 0) {
                    smoothScrollBy(mMapView, dx, 0);
                    return;
                }
                if (dy < 0) {
                    smoothScrollBy(mMapView, 0, dy);
                    return;
                }
                if (dx_right < 0) {
                    smoothScrollBy(mMapView, -dx_right, 0);
                    return;
                }
            }
        }, 500);
    
    }