2013-04-03 15 views
4

它首次呈現時對我來說工作正常。 但是,如果我改變了地圖上的任何東西或重新創建它,它就會被破壞。GWT Google Map Api V3 - 更換時發生故障

下面是屏幕截圖的外觀。 Here

下面是更改每頁結果值後的屏幕截圖。 Here

這是我的代碼。

@UiField DivElement mapPanel; 

private GoogleMap googleMap; 

public void loadAllMarkers(final List<LatLng> markers) 
{ 
    if(!markers.isEmpty()) 
    { 
     final MapOptions options = MapOptions.create(); 
     options.setMapTypeId(MapTypeId.ROADMAP); 

     googleMap = GoogleMap.create(mapPanel, options); 

     final LatLngBounds latLngBounds = LatLngBounds.create(); 

     for(LatLng latLng : markers) 
     { 
     final MarkerOptions markerOptions = MarkerOptions.create(); 

     markerOptions.setPosition(latLng); 
     markerOptions.setMap(googleMap); 

     final Marker marker = Marker.create(markerOptions); 
     latLngBounds.extend(marker.getPosition()); 

     } 

     googleMap.setCenter(latLngBounds.getCenter()); 
     googleMap.fitBounds(latLngBounds); 

    } 
} 

無論何時需要加載新結果,我都會調用loadAllMarkers()方法。

有人能指出我在做什麼錯在這裏。

回答

2

這似乎來自以下(這是我從Google+社羣拉 - GWT Maps V3 API):

布蘭登DonnelsonMar 5,2013

我有這種情況發生,並忘記了爲什麼它是,但是 mapwidget.triggerResize()將重置切片。當onAttach發生並且動畫存在意味着div 開始變小並且側面增加時,這似乎發生 ,但是地圖已經連接了 。在動畫結束時,地圖不會自動調整大小。 我一直在調查自動調整大小的含義,但我還沒有時間去 攻擊它呢。

對於您的情況,您可以在完成更改後調用googleMap.triggerResize()。這解決了我的問題,當我有完全相同的問題。我知道這有點晚,但我希望它有幫助!

另一個答案有延長與以下的地圖窗口小部件:

@Override 
protected void onAttach() { 
    super.onAttach(); 
    Timer timer = new Timer() { 

     @Override 
     public void run() { 
      resize(); 
     } 
    }; 
    timer.schedule(5); 
} 

/* 
* This method is called to fix the Map loading issue when opening 
* multiple instances of maps in different tabs 
* Triggers a resize event to be consumed by google api in order to resize view 
* after attach. 
* 
*/ 
public void resize() { 
    LatLng center = this.getCenter(); 
    MapHandlerRegistration.trigger(this, MapEventType.RESIZE);   
    this.setCenter(center); 
}