2013-04-03 42 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); 
}