Skip to content

Commit

Permalink
Fixed issue with preview for WMS
Browse files Browse the repository at this point in the history
  • Loading branch information
brendan-ward committed Mar 25, 2017
1 parent 82f1c2e commit 8e0b78f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion L.Control.Basemaps-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions L.Control.Basemaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,35 @@ L.Control.Basemaps = L.Control.extend({
z: this.options.tileZ
}, d.options));

if (d instanceof L.TileLayer.WMS) {
// d may not yet be initialized, yet functions below expect ._map to be set
d._map = map;

// unfortunately, calling d.getTileUrl() does not work due to scope issues
// have to replicate some of the logic from L.TileLayer.WMS

// adapted from L.TileLayer.WMS::onAdd
var crs = d.options.crs || map.options.crs;
var wmsParams = L.extend({}, d.wmsParams);
var wmsVersion = parseFloat(wmsParams.version);
var projectionKey = wmsVersion >= 1.3 ? 'crs' : 'srs';
wmsParams[projectionKey] = crs.code;

// adapted from L.TileLayer.WMS::getTileUrl
var coords2 = L.point(coords);
coords2.z = this.options.tileZ;
var tileBounds = d._tileCoordsToBounds(coords2);
var nw = crs.project(tileBounds.getNorthWest());
var se = crs.project(tileBounds.getSouthEast());
var bbox = (wmsVersion >= 1.3 && crs === EPSG4326 ?
[se.y, nw.x, nw.y, se.x] :
[nw.x, se.y, se.x, nw.y]).join(',');

url += L.Util.getParamString(wmsParams, url, d.options.uppercase) +
(d.options.uppercase ? '&BBOX=' : '&bbox=') + bbox;
}


var basemapNode = L.DomUtil.create('div', basemapClass, container);
var imgNode = L.DomUtil.create('img', null, basemapNode);
imgNode.src = url;
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ Include the JavaScript:

The control expects a list of TileLayer instances, constructed in the [normal way](http://leafletjs.com/reference.html#tilelayer).

An optional `layer` property can be added in the options for each basemap, and this will be used to populate the tooltip
An optional `label` property can be added in the options for each basemap, and this will be used to populate the tooltip
(HTML `title` attribute) for that basemap.

Each basemap is represented using a tile from the underlying tile service. Choose the tile x, y, z that provides the
best looking representative basemap image for your application.

`TileLayer.WMS` layers can also be used, and tile coordinates will be converted to bounding boxes to request the preview thumbnail.

The preview shows an alternative basemap to the currently selected basemap to be more apparent as a toggle between basemaps.

Note: this automatically adds the first basemap in your list to the map during initialization, so you don't need to add that
Expand Down Expand Up @@ -80,6 +82,10 @@ var basemaps = [
maxZoom: 16,
minZoom: 1,
label: 'Watercolor'
}),
L.tileLayer.wms('https://maps.ngdc.noaa.gov/soap/web_mercator/gebco08_hillshade/MapServer/WMSServer', {
layers: 'GEBCO_08 hillshade with Natural Earth 2',
label: 'GEBCO Hillshade'
})
];
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Leaflet.Basemaps",
"version": "0.1.2",
"version": "0.1.3",
"homepage": "https://github.com/consbio/Leaflet.Basemaps",
"authors": [
"Brendan Ward <[email protected]>"
Expand Down
4 changes: 4 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
maxZoom: 16,
minZoom: 1,
label: 'Watercolor'
}),
L.tileLayer.wms('https://maps.ngdc.noaa.gov/soap/web_mercator/gebco08_hillshade/MapServer/WMSServer', {
layers: 'GEBCO_08 hillshade with Natural Earth 2',
label: 'GEBCO Hillshade'
})
];

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "leaflet-basemaps",
"version": "0.1.2",
"version": "0.1.3",
"description": "Basemaps Control for Leaflet",
"homepage": "http://github.com/consbio/Leaflet.Basemaps",
"author": {
Expand Down

0 comments on commit 8e0b78f

Please sign in to comment.