Here I give a simple demo how to write map class.
- MapOptions
- Map
1. MapOptions
A JSON object in JavaScript, but in GWT you must write a class extend BaseJavaScriptObject for it. Because it is a simple JSON, so result in the createNew method as following. In this JSON, "resolutions" and "controls" are JavaScript Array, so I must convert Object[] to JavaScript Array then can setProperty.
package org.gwtopenmaps.openlayers.client;
import org.gwtopenmaps.openlayers.client.common.BaseJavaScriptObject;
import org.gwtopenmaps.openlayers.client.common.JavaScriptObjectHelper;
import org.gwtopenmaps.openlayers.client.control.Control;
import com.google.gwt.core.client.JavaScriptObject;
public class MapOptions extends BaseJavaScriptObject {
protected MapOptions() {}
public static MapOptions createNew() {
return (MapOptions) JavaScriptObject.createObject();
}
public final void setProjection(String projection) {
setProperty("projection", projection);
}
public final void setResolutions(Double... resolutions) {
JavaScriptObject javaScriptArray = JavaScriptObjectHelper.toJavaScriptArray((Object[]) resolutions);
setProperty("resolutions", javaScriptArray);
}
public final void setPanRatio(int panRatio) {
setProperty("panRatio", panRatio);
}
public final void setPanDuration(int panDuration) {
setProperty("panDuration", panDuration);
}
public final void setControls(Control... controls) {
JavaScriptObject javaScriptArray = JavaScriptObjectHelper.toJavaScriptArray((Object[]) controls);
setProperty("controls", javaScriptArray);
}
}
2. Map
Because this is a small demo, so just see the code following...
package org.gwtopenmaps.openlayers.client;
import org.gwtopenmaps.openlayers.client.common.BaseJavaScriptObject;
import org.gwtopenmaps.openlayers.client.control.Control;
import org.gwtopenmaps.openlayers.client.layer.Layer;
public class Map extends BaseJavaScriptObject {
protected Map() {
}
public static native Map createNew(String divId, MapOptions mapOptions) /*-{
return new $wnd.OpenLayers.Map(divId, mapOptions);
}-*/;
public final void addLayer(Layer layer) {
invokeMethodReturnJavaScriptObject("addLayer", layer);
}
public final void setCenter(LonLat lonLat, int zoom) {
invokeMethodReturnJavaScriptObject("setCenter", lonLat, zoom);
}
public final Control getControl(String id) {
return (Control) invokeMethodReturnJavaScriptObject("getControl", id);
}
public final int getZoom() {
return invokeMethodReturnInt("getZoom");
}
}
to be continued...