我目前正在用 java 制作一个类,允许我查看谷歌地图并计算距离。在这一点上,我想做的就是查看地图。谁能帮我?
这是我的代码:
public class MapOperations{
private GoogleMap map;
/*Import Club's School and set center accordingly*/
private static final LatLng WEST = LatLng.create(42.02606, -87.77194);
private static final LatLng NORTH = LatLng.create(41.850033, -87.6500523);
public MapOperations(){
try{
init();
}
catch(Exception e){
System.out.println(e);
}
}
public void init() {
MapOptions mapOpts = MapOptions.create();
mapOpts.setZoom(16);
mapOpts.setCenter(WEST);
mapOpts.setMapTypeControl(true);
MapTypeControlOptions mapTypeCtrlOpts = MapTypeControlOptions.create();
mapTypeCtrlOpts.setMapTypeIds(ArrayHelper.toJsArrayString(MapTypeId.ROADMAP.getValue(), MapTypeId.HYBRID.getValue()));
mapTypeCtrlOpts.setStyle(MapTypeControlStyle.DROPDOWN_MENU);
mapOpts.setMapTypeControlOptions(mapTypeCtrlOpts);
mapOpts.setZoomControl(true);
ZoomControlOptions zoomOpts = ZoomControlOptions.create();
zoomOpts.setStyle(ZoomControlStyle.SMALL);
mapOpts.setZoomControlOptions(zoomOpts);
mapOpts.setMapTypeId(MapTypeId.ROADMAP);
map = GoogleMap.create(Document.get().getElementById("map_canvas"), mapOpts);
Button btn = new Button("Reset Center", new ClickHandler() {
public void onClick(ClickEvent event) {
map.setCenter(WEST);
map.setZoom(16);
}
});
// Add button to the root panel. (register it on the GWT side)
RootPanel.get().add(btn);
// Add button as a map control.
map.getControls().get(
new Double(ControlPosition.TOP_RIGHT.getValue()).intValue()).push(
btn.getElement());
}
}
当我尝试运行我的代码时,我收到了这个错误:
com.google.gwt.core.client.JavaScriptException: (TypeError) @com.google.maps.gwt.client.GoogleMap::create(Lcom/google/gwt/dom/client/Node;Lcom/google/maps/gwt/client/MapOptions;)([null, JavaScript object(1433)]): Cannot read property 'offsetWidth' of null