Skip to content

Instantly share code, notes, and snippets.

Created March 26, 2015 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/f6e7dc3d4eff9e0ed62b to your computer and use it in GitHub Desktop.
Save anonymous/f6e7dc3d4eff9e0ed62b to your computer and use it in GitHub Desktop.
better_wms.js
L.TileLayer.BetterWMS = L.TileLayer.WMS.extend({
onAdd: function (map) {
// Triggered when the layer is added to a map.
// Register a click listener, then do all the upstream WMS things
L.TileLayer.WMS.prototype.onAdd.call(this, map);
map.on('click', this.getFeatureInfo, this);
},
onRemove: function (map) {
// Triggered when the layer is removed from a map.
// Unregister a click listener, then do all the upstream WMS things
L.TileLayer.WMS.prototype.onRemove.call(this, map);
map.off('click', this.getFeatureInfo, this);
},
getFeatureInfo: function (evt) {
// Make an AJAX request to the server and hope for the best
var url = this.getFeatureInfoUrl(evt.latlng),
showResults = L.Util.bind(this.showGetFeatureInfo, this);
$.ajax({
url: url,
success: function (data, status, xhr) {
// ********************************** if info-FORMAT is json type is an object!
var err = typeof data === 'object' ? null : data;
showResults(err, evt.latlng, data);
},
error: function (xhr, status, error) {
showResults(error);
}
});
},
getFeatureInfoUrl: function (latlng) {
// Construct a GetFeatureInfo request URL given a point
var point = this._map.latLngToContainerPoint(latlng, this._map.getZoom()),
size = this._map.getSize(),
params = {
request: 'GetFeatureInfo',
service: 'WMS',
srs: 'EPSG:4326',
styles: this.wmsParams.styles,
transparent: this.wmsParams.transparent,
version: this.wmsParams.version,
format: this.wmsParams.format,
bbox: this._map.getBounds().toBBoxString(),
height: size.y,
width: size.x,
layers: this.wmsParams.layers,
query_layers: this.wmsParams.layers,
// INFO FORMAT JSON
info_format: 'application/json',
propertyName:'LAND,DATUM'
};
params[params.version === '1.3.0' ? 'i' : 'x'] = point.x;
params[params.version === '1.3.0' ? 'j' : 'y'] = point.y;
return this._url + L.Util.getParamString(params, this._url, true);
},
showGetFeatureInfo: function (err, latlng, content) {
if (err) {
// console.log(err);
return;
} // do nothing if there's an error
//console.log(content);
if (content.features.length>0)
{
// Otherwise show the content in a popup, or something.
L.popup({ maxWidth: 800})
.setLatLng(latlng)
.setContent(buildpopup(content))
.openOn(this._map);
}
else
{ // Optional... show an error message if no feature was returned
$("#daneben").fadeIn(750);
setTimeout(function(){ $("#daneben").fadeOut(750); }, 2000);
}
}
});
L.tileLayer.betterWms = function (url, options) {
return new L.TileLayer.BetterWMS(url, options);
};
function buildpopup(content){
var record;
var info = "<div class=\"mypopupinfo\">";
for (var i=0 ; i < content.features.length; i++ ){
record = content.features[i];
info += "<div class=\"popupinfo\"><table>"
info += "<tr><th><b>Land: </b></th><th><b>Datum: </b></th></tr>";
if(record.properties.LAND=="Nds")
{
record.properties.LAND="Niedersachsen";
}
info += "<tr><td>" + record.properties.LAND + "</td><td>" + record.properties.DATUM + "</td></tr>";
info+="</table></div>";
if (i!= (content.features.length-1)){
info += "<br />";
}
}
info += "</div>"
return info;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment