Java tutorial
package topshelf.gwt.leaflet.client; import java.util.List; import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.Scheduler; import com.google.gwt.dom.client.Document; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.LinkElement; import com.google.gwt.dom.client.ScriptElement; import com.google.gwt.user.client.Command; import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.ui.Label; public class Leaflet extends Label { private static final String defaultAttribution = "Map data © <a href=\"http://openstreetmap.org\">OpenStreetMap</a> contributors, <a href=\"http://creativecommons.org/licenses/by-sa/2.0/\">CC-BY-SA</a>, Imagery <a href=\"http://cloudmade.com\">CloudMade</a>"; private static String apiKey = null; private static String proxyUrl = null; private static String leafletBase = GWT.getModuleName(); public static void init(String api) { init(api, null, null); } public static void init(String api, final Command callback) { init(api, null, callback); } public static void init(String api, String proxyUrl) { init(api, proxyUrl, null); } public static void init(String api, String proxUrl, final Command callback) { proxyUrl = proxUrl; apiKey = api; ScriptElement script = Document.get().createScriptElement(); script.setSrc("/" + leafletBase + "/cloudmade-leaflet/leaflet.js"); script.setType("text/javascript"); Document.get().getBody().getParentElement().appendChild(script); LinkElement link = Document.get().createLinkElement(); link.setRel("stylesheet"); link.setHref("/" + leafletBase + "/cloudmade-leaflet/leaflet.css"); Element firstLink = Document.get().getElementsByTagName("link").getItem(0); firstLink.getParentNode().insertBefore(link, firstLink); if (null != callback) new ScriptTimer(callback).check(); } private static abstract class AbstrLoadedTimer { private Timer timer; abstract boolean isReady(); public AbstrLoadedTimer(final Command callback) { timer = new Timer() { @Override public void run() { if (isReady()) { Scheduler.get().scheduleDeferred(callback); } else { schedule(10); } } }; } public void check() { timer.run(); } } private static class ScriptTimer extends AbstrLoadedTimer { ScriptTimer(Command callback) { super(callback); } @Override boolean isReady() { return _loaded(); } } private static class AttachedTimer extends AbstrLoadedTimer { Leaflet inst; AttachedTimer(Command callback, Leaflet inst) { super(callback); this.inst = inst; } @Override boolean isReady() { return _loaded() && inst.isAttached(); } } private static class UseMapTimer extends AttachedTimer { UseMapTimer(Command callback, Leaflet inst) { super(callback, inst); } @Override boolean isReady() { return super.isReady() && inst._mapReady(); } } private native boolean _mapReady() /*-{ var map = this.@topshelf.gwt.leaflet.client.Leaflet::leafletMap; return (map != undefined); }-*/; private static native boolean _loaded() /*-{ return ($wnd.L != undefined); }-*/; JavaScriptObject leafletMap; JavaScriptObject markersMap; JavaScriptObject markerGroupsMap; public Leaflet() { this(options(18, null)); } public Leaflet(String attribution) { this(options(18, attribution)); } public Leaflet(int maxZoomLevel, String attribution) { this(options(maxZoomLevel, attribution)); } public static class Options { int maxZoomLevel; String attribution; boolean scrollWheelZoom = true; boolean touchZoom = true; private Options(int maxZoomLevel, String attribution) { this.maxZoomLevel = maxZoomLevel; this.attribution = attribution; } public Options touchZoom(boolean flag) { this.touchZoom = flag; return this; } public Options scrollWheelZoom(boolean flag) { this.scrollWheelZoom = flag; return this; } } public static Options options(int maxZoomLevel, String attribution) { return new Options(maxZoomLevel, attribution); } public Leaflet(final Options opts) { super(); if (null == apiKey) throw new RuntimeException("Must be initialized with API key!"); final Element div = getElement(); new AttachedTimer(new Command() { @Override public void execute() { _map(div, apiKey, opts); } }, this).check(); } private void _map(Element mapelement, String api, Options opts) { String styleId = "997"; String tileSize = "256"; String cmUrl = "http://{s}.tile.cloudmade.com/" + api + "/" + styleId + "/" + tileSize + "/{z}/{x}/{y}.png"; if (null == proxyUrl) { _map(cmUrl, mapelement, opts); } else { _map(proxyUrl + "?url=" + cmUrl, mapelement, opts); } } private native void _map(String url, Element mapelement, Options opt) /*-{ var a = opt.@topshelf.gwt.leaflet.client.Leaflet.Options::attribution; var mzl = opt.@topshelf.gwt.leaflet.client.Leaflet.Options::maxZoomLevel; var swz = opt.@topshelf.gwt.leaflet.client.Leaflet.Options::scrollWheelZoom; var tz = opt.@topshelf.gwt.leaflet.client.Leaflet.Options::touchZoom; var map = new $wnd.L.Map(mapelement); var cloudmade = new $wnd.L.TileLayer(url, { attribution : a, maxZoom : mzl, scrollWheelZoom : swz, touchZoom: tz }); map.addLayer(cloudmade); this.@topshelf.gwt.leaflet.client.Leaflet::leafletMap = map; this.@topshelf.gwt.leaflet.client.Leaflet::markerGroupsMap = new Object(); this.@topshelf.gwt.leaflet.client.Leaflet::markersMap = new Object(); }-*/; public void remove(List<Marker> markers) { for (Marker m : markers) remove(m); } public void remove(Marker marker) { _removeMarker(marker.getId()); } public void clearLayerGroup(final String groupName) { new UseMapTimer(new Command() { @Override public void execute() { _clearLayerGroup(groupName); } }, this).check(); } private native void _clearLayerGroup(String groupName) /*-{ var group = this.@topshelf.gwt.leaflet.client.Leaflet::markerGroupsMap[groupName]; if (null != group) group.clearLayers(); }-*/; private native void _removeMarker(Object id) /*-{ var map = this.@topshelf.gwt.leaflet.client.Leaflet::leafletMap; var markers = this.@topshelf.gwt.leaflet.client.Leaflet::markersMap; var marker = markers[id]; map.removeLayer(marker); }-*/; public void view(String groupName, Marker marker) { view(groupName, marker, false, -1); } public static interface ZoomHandler { int getZoom(double latRange, double lngRange); } public void view(String groupName, List<? extends Marker> markers, boolean center, ZoomHandler zh) { double left = 0; double right = 0; double top = 0; double bottom = 0; int cnt = 0; for (Marker marker : markers) { double lat = marker.getLatLng().getLat(); double lng = marker.getLatLng().getLng(); if (0 == cnt) { left = lng; right = lng; top = lat; bottom = lat; } else { if (left > lng) left = lng; if (right < lng) right = lng; if (top < lat) top = lat; if (bottom > lat) bottom = lat; } cnt++; } double latRange = top - bottom; double lngRange = right - left; int zoom = zh.getZoom(latRange, lngRange); if (center) { double latCenter = top - (latRange / 2); double lngCenter = right - (lngRange / 2); _setView(latCenter, lngCenter, zoom); } else { _setZoom(zoom); } for (Marker marker : markers) { view(groupName, marker, false); } } public void view(String groupName, Marker marker, boolean center) { view(groupName, marker, center, this.zoom); } public void view(final String groupName, final Marker marker, final boolean center, final int zoom) { new UseMapTimer(new Command() { @Override public void execute() { _view(groupName, marker, center, zoom); } }, this).check(); } private void _view(String groupName, Marker marker, boolean center, int zoom) { LatLng latLng = marker.getLatLng(); Icon icon = marker.getIcon(); _addLayer(groupName, marker.getId(), marker.getTitle(), latLng.getLat(), latLng.getLng(), icon.getIconUrl(), icon.getIconPoint().getX(), icon.getIconPoint().getY(), icon.getShadowUrl(), icon.getShadowPoint().getX(), icon.getShadowPoint().getY(), icon.getIconAnchor().getX(), icon.getIconAnchor().getY()); if (center) { _setView(latLng.getLat(), latLng.getLng(), zoom); } } private int zoom; private void _setView(double lat, double lng, int zoom) { this.zoom = zoom; __setView(lat, lng, zoom); } private native void __setView(double lat, double lng, int zoom) /*-{ var map = this.@topshelf.gwt.leaflet.client.Leaflet::leafletMap; if (null != map) { var loc = new $wnd.L.LatLng(lat, lng); // geographical point (longitude and latitude) map.setView(loc, zoom); } }-*/; private native void _setZoom(int zoom) /*-{ var map = this.@topshelf.gwt.leaflet.client.Leaflet::leafletMap; if (null != map) { map.setZoom(zoom); } }-*/; private native void _addLayer(String groupName, Object id, String title, double lat, double lng, String iconUrl, int iconX, int iconY, String shadowUrl, int shadowX, int shadowY, int iconAnchorX, int iconAnchorY) /*-{ var map = this.@topshelf.gwt.leaflet.client.Leaflet::leafletMap; var group = this.@topshelf.gwt.leaflet.client.Leaflet::markerGroupsMap[groupName]; if (null == group) { var group = new $wnd.L.LayerGroup(); map.addLayer(group); this.@topshelf.gwt.leaflet.client.Leaflet::markerGroupsMap[groupName] = group; } var DefaultIcon = $wnd.L.Icon.extend({ iconUrl : iconUrl, shadowUrl : shadowUrl, iconSize : new $wnd.L.Point(iconX, iconY), shadowSize : new $wnd.L.Point(shadowX, shadowY), iconAnchor : new $wnd.L.Point(iconAnchorX, iconAnchorY) }); var loc = new $wnd.L.LatLng(lat, lng); // geographical point (longitude and latitude) var defaultIcon = new DefaultIcon(); var marker = new $wnd.L.Marker(loc, { icon : defaultIcon }); group.addLayer(marker); var markersMap = this.@topshelf.gwt.leaflet.client.Leaflet::markersMap; markersMap[id] = marker; }-*/; }