1

I'm trying to add a view over the marker in the google maps fragment I searched everywhere even in the old deprecated functions, but it seems no matter what method I'm using the maps will always call canvas.draw() on the view I'm creating converting it to a bitmap "Picture", but I want to show a bubble over the marker that contains a textview , input text from the user not to show some text

is there any possiable way to do this?

note: I'm trying to avoid creating view and putting it over the fragment in a realtivelayout because I'm trying to avoid calculating where the marker is if that's even possible

I would have put some of my code but I can't decide which is relevant here!

Hassan Khallouf
  • 1,170
  • 1
  • 13
  • 30

1 Answers1

1

Unfortunately, AFAIK with the current version of Maps v2 api there is no other way to do that than by monitoring marker position.

Below is sample code how you can do this.

I used this aproach in my own app and it works fine.

AbsoluteLayout.LayoutParams overlayLayoutParams;

ViewGroup infoWindowContainer; // your custom info window


private void showInfoWindow(Marker marker) {
    /*
        prepare & show your custom info window 
    */
    trackedPosition = marker.getPosition();
    startPositionUpdater();
}

private void hideInfoWindow() {
    /*
        hide your custom info window
    */
    stopPositionUpdater();
}


private void startPositionUpdater() {
    if (trackedPosition != null && infoWindowContainer.getVisibility() == VISIBLE && map != null) {
        positionUpdaterRunnable = new PositionUpdaterRunnable();
        handler.post(positionUpdaterRunnable);
    }
}

private void stopPositionUpdater() {
    if(positionUpdaterRunnable != null) {
        handler.removeCallbacks(positionUpdaterRunnable);
        positionUpdaterRunnable = null;
    }
}    


private class PositionUpdaterRunnable implements Runnable {
    private int lastXPosition = Integer.MIN_VALUE;
    private int lastYPosition = Integer.MIN_VALUE;

    @Override
    public void run() {
        if (trackedPosition != null && isInfoWindowShown() && map != null && handler != null) {

            Projection projection = map.getProjection();
            Point targetPosition = projection.toScreenLocation(trackedPosition);

            if (lastXPosition != targetPosition.x || lastYPosition != targetPosition.y) {
                overlayLayoutParams.x = targetPosition.x - popupXOffset;
                overlayLayoutParams.y = targetPosition.y - popupYOffset;
                infoWindowContainer.setLayoutParams(overlayLayoutParams);

                lastXPosition = targetPosition.x;
                lastYPosition = targetPosition.y;
            }

            handler.postDelayed(this, INFO_WINDOW_POSITION_REFRESH_INTERVAL);
        }
    }
}
Kamil
  • 336
  • 1
  • 7