Web Map Service (WMS) is a way of publishing local geodata as map tiles, but more opetational. It can be published by GeoServer or other softwares e.g. QGIS. Leaflet.js is a open source JS library, which is made for interacitve maps. With Mapbox, more maps can be designed, personalised and added to web applications via Leaflet.js.
Insert js libraries
Download the libs in your local folder, and include them in your HTML file.
<head> <!-- down load Leaflet lib from internet --> <linkrel="stylesheet"href="libs/leaflet.css"/> <scriptsrc="libs/leaflet.js"></script> </head> <divid="map"></div> <script> //OSM as base map var OpenStreetMap = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'); //map configuration var map = L.map('map', { layers: [OpenStreetMap], center: [31.2, 121.4], // default center coordinate, here is Shanghai zoom: 3// default zoom level }); </script>
WMS Layer
1 2 3 4
// add WMS to map var wmsLayer = L.tileLayer.wms('http://localhost:8080/geoserver/first/wms?', { layers: 'first:railways_cn', }).addTo(map);
WMS is loaded as tailed photos, the default format is .jepg, so that the layer can not have transparent band. The upper layers will cover the base map. Change the tailed photo’s format and set transparency:
Leaflet supports very few coordinate systems: CRS:3857, CRS:3395 and CRS:4326. If your WMS service doesn’t serve images in those coordinate systems, you might need to use Proj4Leaflet to use a different coordinate system in Leaflet.
Or set the layer’s coordinates in GeoServer.
Layer control
Base map
1 2 3 4 5 6 7 8 9 10 11
// base map server url var OpenStreetMap = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'); var WorldImagery = L.tileLayer('http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}');
//add layer to a attribute var baseLayers = { "Open Street Map": OpenStreetMap, "World Imagery": WorldImagery };
// Group layers var overlays = { "points": points, "railways": railways, "buildings": buildings, "landuse": landuse, "boundary": boundary, };
// add base maps and overlays together to the map L.control.layers(baseLayers,overlays).addTo(map);
Widgets
Search function
1 2
var osmGeocoder = new L.Control.OSMGeocoder(); map.addControl(osmGeocoder);
Overview Map
1 2
var osm2 = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'); var miniMap = new L.Control.MiniMap(osm2, { toggleDisplay: true }).addTo(map);