GWT MapWidget从JavaScript作为GMap2 [英] GWT MapWidget from javascript as GMap2
问题描述
我正在使用GWT进行Google地图应用。我使用Google的 gwt-maps.jar 映射api。
I am using GWT for google map application. I use Google's gwt-maps.jar for mapping api.
我可以创建地图为
I can create a map as
private MapWidget mapId;
if (Maps.isBrowserCompatible()) {
mapId = new MapWidget();
mapId.setPixelSize(600, 500);
RootPanel.get("gmap").add(mapId);
mapId.setCurrentMapType(MapType.getHybridMap());
mapId.addControl(new LargeMapControl());
mapId.addControl(new MapTypeControl());
mapId.addControl(new ScaleControl());
mapId.setCenter(LatLng.newInstance(-33.867139, 151.20711), 9);
}
现在我有一些遗留的JavaScript代码需要在这个地图对象上工作。我想用这样的东西:
Now I have some legacy javascript code that needed to work on this map object. I want to use something like this:
$wnd.mapId.addOverlay(ovrlay1);
但是我无法得到这个$ wnd.mapId变量。你知道有什么方法从某个元素上的现有地图获取GMap2对象吗?它会工作,如果我从我的gmapdiv获得GMap2实例。
But I cannot get this $wnd.mapId variable. Do you know any way to get GMap2 object from existing map on some element? It will work if I get the GMap2 instance from my "gmap" div.
目前我正在使用JSNI来生成所有地图功能。 Simmilar to:
For the time being I am using JSNI to generate all the map functionalities. Simmilar to:
$wnd.mapId = new $wnd.GMap2($wnd.document.getElementById("gmap"));
推荐答案
看起来我必须自己回答。 MapWidget对象可以使用getPeer()方法返回JavaScript对象的句柄。所以我需要通过调用getPeer来绑定一个JavaScript对象。以下代码执行此操作。
Looks like I have to answer this by myself. The MapWidget object can return an handle to javascript object using getPeer() method. So What I need is bind a javascript object by calling getPeer. The following code do this.
private MapWidget mapId;
if (Maps.isBrowserCompatible()) {
mapId = new MapWidget();
nativeExportMapPeer(mapId.getPeer());
...
}
...
// export the native GMap2 object with a JSNI method
private static native void nativeExportMapPeer(JavaScriptObject peer) /*-{
$wnd.mapId = peer;
}-*/;
感谢Eric Z. Ayers指出这一点。
Thanks Eric Z. Ayers for pointing this.
这篇关于GWT MapWidget从JavaScript作为GMap2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!