openlayers - Get a feature info on map click in openlayers3 javascript - Stack Overflow

i am trying to get feature information on map click using openlayers 3 javascript with the help of the

i am trying to get feature information on map click using openlayers 3 javascript with the help of the example popupinfo

   
  var mmi =  new ol.layer.Tile({
      source: new ol.source.OSM()
    });
	
  var one =  new ol.layer.Image({
        source: new ol.source.ImageWMS({
        url: 'http://localhost:8080/geoserver/wms',
        params: {'LAYERS': 'cite:abc'},
		format: new ol.format.GeoJSON(),
        ratio: 1,
        serverType: 'geoserver'
      })
    });
    		  
  var map = new ol.Map({
    layers: [mmi,one],
    target: 'map',
    view: new ol.View({
    center: ol.proj.fromLonLat([73.6608, 29.8820]),
      zoom: 8
    })
	});
	

  map.on('click', function(evt) {
   var feature = map.forEachFeatureAtPixel(evt.pixel, function(feature, one) 
 {
return feature;
   })
  });
<link href=".6.4/css/ol.css" rel="stylesheet"/>
<script src=".6.4/build/ol-debug.js"></script>
<div id="map" class="map"></div>
<div id="map" class="information"></div>

i am trying to get feature information on map click using openlayers 3 javascript with the help of the example popupinfo

   
  var mmi =  new ol.layer.Tile({
      source: new ol.source.OSM()
    });
	
  var one =  new ol.layer.Image({
        source: new ol.source.ImageWMS({
        url: 'http://localhost:8080/geoserver/wms',
        params: {'LAYERS': 'cite:abc'},
		format: new ol.format.GeoJSON(),
        ratio: 1,
        serverType: 'geoserver'
      })
    });
    		  
  var map = new ol.Map({
    layers: [mmi,one],
    target: 'map',
    view: new ol.View({
    center: ol.proj.fromLonLat([73.6608, 29.8820]),
      zoom: 8
    })
	});
	

  map.on('click', function(evt) {
   var feature = map.forEachFeatureAtPixel(evt.pixel, function(feature, one) 
 {
return feature;
   })
  });
<link href="https://openlayers/en/v4.6.4/css/ol.css" rel="stylesheet"/>
<script src="https://openlayers/en/v4.6.4/build/ol-debug.js"></script>
<div id="map" class="map"></div>
<div id="map" class="information"></div>

on my map click event the feature value is nothing. how to get the value of feature on clicking on it.

Share Improve this question edited May 4, 2018 at 12:00 vaibhav Dhiman asked May 3, 2018 at 6:46 vaibhav Dhimanvaibhav Dhiman 192 silver badges9 bronze badges 3
  • Please fix the pilation errors first, there are way too many! Your formatting is also off and a likely cause for some of the mistakes. overlay is not defined, props of map are multiplied, hdms is undefined, ++i should be i++. displayFeatureInfo is defined inside the singleclick listener, ends too early and is never called. content is undefined, coordinate is undefined – dube Commented May 3, 2018 at 21:50
  • i am not getting any pilation errors.. map.forEachFeatureAtPixel(evt, function(feature, one) – vaibhav Dhiman Commented May 4, 2018 at 4:25
  • I converted your code dump into an executable snipped. Take a look and fix the errors. – dube Commented May 4, 2018 at 6:21
Add a ment  | 

2 Answers 2

Reset to default 3

You are studying the wrong example for your use case, you need a getFeatureInfo call as you are using a Web Map Service (WMS).

map.on('click', function(evt) {
      var url = wms_layer.getSource().getFeatureInfoUrl(
          evt.coordinate, viewResolution, viewProjection,
          {'INFO_FORMAT': 'text/html',
           'propertyName': 'formal_en'});
      if (url) {
        var parser = new ol.format.GeoJSON();
        $.ajax({
          url: url,
        
        }).then(function(response) {
            container.innerHTML = response;
          } else {
            container.innerHTML = '&nbsp;';
          }
        });
      }
    });

You can get feature information like this:

map.on("singleclick", function (evt) {
  this.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
    console.log(feature.get("<property_key>"));
  });
});

Example Code:

var vectorSource = new ol.source.Vector({
  format: new ol.format.GeoJSON(),
  url: function(extent) {
    return 'https://ahocevar./geoserver/wfs?service=WFS&' +
        'version=1.1.0&request=GetFeature&typename=osm:water_areas&' +
        'outputFormat=application/json&srsname=EPSG:3857&' +
        'bbox=' + extent.join(',') + ',EPSG:3857';
  },
  strategy: ol.loadingstrategy.bbox
});


var vector = new ol.layer.Vector({
  source: vectorSource,
  style: new ol.style.Style({
    stroke: new ol.style.Stroke({
      color: 'rgba(0, 0, 255, 1.0)',
      width: 2
    })
  })
});

var raster = new ol.layer.Tile({
  source: new ol.source.BingMaps({
    imagerySet: 'Aerial',
    key: 'Your Bing Maps Key from http://www.bingmapsportal./ here'
  })
});

var map = new ol.Map({
  layers: [raster, vector],
  target: document.getElementById('map'),
  view: new ol.View({
    center: [-8908887.277395891, 5381918.072437216],
    maxZoom: 19,
    zoom: 12
  })
});

map.on("singleclick", function (evt) {
  this.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
    alert("Osm Id: " + feature.get("osm_id") + "\nLand Use: " + feature.get("landuse"));
  });
});
<script src="https://openlayers/en/v4.6.5/build/ol.js"></script>
<link href="https://openlayers/en/v4.6.5/css/ol.css" rel="stylesheet"/>
<div id="map" class="map"  style="width: 98%; height: calc(95%); position: absolute;"></div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745180710a4615405.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信