简体   繁体   中英

geoserver / leaflet / getfeatureinfo

I have a very simple question and i could not so far find a solution. I have a leaflet which gets data from Geoserver via L.TileLayer.BetterWMS.js ( https://gist.github.com/rclark/6908938 ). When a user click in the image, get the feature data. In the WMS there is only a row each time with only one value. I would like to get this value to a variable and parse to PHP. How to do that? Below is a picture of the leaflet and the table which I get.

地理服务器getFeatureInfo

I want to parse the value 188 to a variable.

Thanks in advance!

如果您想访问这些值,那么将以text / html的形式返回您的响应,您应该在application / json或application / xml中请求数据并进行解析。

Guys thanks and I found the solution. You can create a new DomParser and get a string. After creating an element and parse the html you got before, you pass the variable like:

el.getElementsByTagName('td')[1].innerHTML;

And that's it. Full code here:

var doc = (new DOMParser()).parseFromString(data, "text/html"); 
if (doc.body.innerHTML.trim().length > 0)
showResults(err, evt.latlng, data);
var el = document.createElement( 'html' );
el.innerHTML = data;
var stra = el.getElementsByTagName('td')[1].innerHTML;

Thanks again!

I have an other solution you need to change first text/html to application/json in L.TileLayer.BetterWMS.js and then you add :

$("#yourId").text(data.features[0].properties.YourVar)

So you finally get :

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) {

    $("#yourid").text(data.features[0].properties.YourVar)


    var err = typeof data === 'string' ? null : data;
    showResults(err, evt.latlng, data);

  },
  error: function (xhr, status, error) {
    showResults(error);  
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM