Malte
Malte

Reputation: 152

second call of google maps does not show the map in correct size

using gwt-maps-3.8.0 i display a route in a gwt popup. Works when called once but does not work on second call.

What should i do ... some advice to refresh the mapWidget? defect map

Upvotes: 3

Views: 2642

Answers (3)

H Ser
H Ser

Reputation: 21

I had the same issue (map shown in a popup; reload the popup and the map was no longer centered).
In the end I managed to fix my problem using the triggerResize method from the GoogleMap class. However it worked only after I triggered this method from an Idle event.
triggerResize will notify the map to show the correct tiles.
setCenter will make sure the map is centered once again.

gMap.addIdleListenerOnce(new IdleHandler() {
    @Override
    public void handle() {
        gMap.triggerResize();
        gMap.setCenter(myLatLng);
    }
});

Upvotes: 2

Joseph Lust
Joseph Lust

Reputation: 19985

Using the GWT-V3-Maps-API it would be done as follows for a case where a div or window resizes:

    /*
     * Example of how to dynamically resize the map to fit the window - add
     * your events
     */
    Window.addResizeHandler(new ResizeHandler() {
        @Override
        public void onResize(ResizeEvent event) {
            MapHandlerRegistration.trigger(mapWidget, MapEventType.RESIZE);
            GWT.log("Window has been resized!");
        }
    });

    mapWidget.addResizeHandler(new ResizeMapHandler() {
        @Override
        public void onEvent(ResizeMapEvent event) {
            GWT.log("Map has been resized!");
        }
    });

Upvotes: 1

Andrew Leach
Andrew Leach

Reputation: 12973

When you display the map, trigger its resize event.

From the documentation:

Developers should trigger this event on the map when the div changes size: google.maps.event.trigger(map, 'resize')

It appears the way to do this in GWT is

Event.trigger(mapWidget.getMap(), "resize");

At the moment, the map has zero size as far as the API is concerned, so it's just displaying the buffer of tiles around the single pixel at (0,0). Triggering the resize event causes the API to get the correct size from the browser so the right tiles are fetched for display.

Upvotes: 5

Related Questions