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);
}
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);
But I cannot get this $wnd.ma开发者_运维技巧pId 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.
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"));
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;
}-*/;
Thanks Eric Z. Ayers for pointing this.
精彩评论