B.Termeer
B.Termeer

Reputation: 315

openlayer 3.5 get feature id from point

I m still learning openlayers and i have a problem. I need for my script the id of a point if this is clicked. With this id(that function as a variable) i want to do some coding.

With help of some examples i manage to get a table on my website.I do this with the following code(javascript):

map.on('singleclick', function(evt) {
    document.getElementById('nodelist').innerHTML = "Loading... please wait...";
    var view = map.getView();
    var viewResolution = view.getResolution();
    var source = untiled.get('visible') ? untiled.getSource() : tiled.getSource();
    var url = source.getGetFeatureInfoUrl(
      evt.coordinate, viewResolution, view.getProjection(),
      {'INFO_FORMAT': 'text/html', 'FEATURE_COUNT': 50});
    if (url) {
      document.getElementById('nodelist').innerHTML = '<iframe seamless src="' + url + '"></iframe>';
    }
  });

But what i want is not to display the table and get the id of the point out of the table.

Can somebody give me some tips?

Upvotes: 1

Views: 800

Answers (1)

ahocevar
ahocevar

Reputation: 5648

If you want access to feature ids, you cannot use the 'text/html' format in your GetFeatureInfo request. Instead, you will have to use a 'application/vnd.ogc.gml' or 'application/json' format. This will only work if the server allows CORS requests, or if the server is on the same origin as your application. Then, instead of using the url from source.getGetFeatureInfoUrl() in an IFRAME, you will have to use it in an AJAX request. Parse the response using ol.format.WMSGetFeatureInfo or ol.format.GeoJSON, and for the parsed features you can then get the id by simply using ol.Feature#getId().

Upvotes: 1

Related Questions