How to create OpenLayers map using GWT?

 

GWT is a web toolkit provided by Google. This toolkit also provides facility of map using openlayers library. Include openlayers jar in your project and then load the map using java code. This is pure JAVA plugins we do not need to include any javascript library.

GWT-OpenLayers takes full advantage of the amazing OpenLayers JavaScript API.

 

Where can I get it?

Maven Developers

<dependency>

        <groupId>org.gwtopenmaps.openlayers</groupId>
        <artifactId>gwt-openlayers-client</artifactId>
        <version>1.0-SNAPSHOT</version>
</dependency>

And the third option is to directly download the JAR and then include this JAR in your GWT web project.

 

Below is code to create the map using GWT Openlayers.

 

private MapWidget mapWidget;
private Map map;
private Vector vectorLayer;
private Markers markerLayer;

 

MapOptions defaultMapOptions = new MapOptions();
defaultMapOptions.setNumZoomLevels(16);

mapWidget = new MapWidget(“100%”, “100%”, defaultMapOptions);
map = mapWidget.getMap();

 

vectorLayer = new Vector(“Vector”);

MarkersOptions markersOptions = new MarkersOptions();
markerLayer = new Markers(“Markers”, markersOptions);

 

StyleMap styleMap = new StyleMap(style, selectStyle, hoverStyle);
vectorLayer.setStyleMap(styleMap);

initMapLayers(map);

map.addLayer(vectorLayer);
map.addLayer(markerLayer);

 

private void initMapLayers(Map map) {
map.addLayer(OSM.Mapnik(“OpenStreetMap”));

GoogleV3Options gHybridOptions = new GoogleV3Options();
gHybridOptions.setNumZoomLevels(20);
gHybridOptions.setType(GoogleV3MapType.G_HYBRID_MAP);
map.addLayer(new GoogleV3(“Google Hybrid”, gHybridOptions));

GoogleV3Options gNormalOptions = new GoogleV3Options();
gNormalOptions.setNumZoomLevels(22);
gNormalOptions.setType(GoogleV3MapType.G_NORMAL_MAP);
map.addLayer(new GoogleV3(“Google Normal”, gNormalOptions));

GoogleV3Options gSatelliteOptions = new GoogleV3Options();
gSatelliteOptions.setNumZoomLevels(20);
gSatelliteOptions.setType(GoogleV3MapType.G_SATELLITE_MAP);
map.addLayer(new GoogleV3(“Google Satellite”, gSatelliteOptions));

GoogleV3Options gTerrainOptions = new GoogleV3Options();
gTerrainOptions.setNumZoomLevels(16);
gTerrainOptions.setType(GoogleV3MapType.G_TERRAIN_MAP);
map.addLayer(new GoogleV3(“Google Terrain”, gTerrainOptions));

final String bingKey = “AseEs0DLJhLlTNoxbNXu7DGsnnH4UoWuGue7-irwKkE3fffaClwc9q_Mr6AyHY8F”;
map.addLayer(new Bing(new BingOptions(“Bing Road”, bingKey, BingType.ROAD)));
map.addLayer(new Bing(new BingOptions(“Bing Hybrid”, bingKey, BingType.HYBRID)));
map.addLayer(new Bing(new BingOptions(“Bing Aerial”, bingKey, BingType.AERIAL)));
}