【问题标题】:Get a feature info on map click in openlayers3 javascript在openlayers3 javascript中获取地图上的功能信息
【发布时间】:2018-05-03 06:46:00
【问题描述】:

我正在尝试在示例popupinfo 的帮助下使用 openlayers 3 获取地图点击上的特征信息 javascript

   
  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.org/en/v4.6.4/css/ol.css" rel="stylesheet"/>
<script src="https://openlayers.org/en/v4.6.4/build/ol-debug.js"></script>
<div id="map" class="map"></div>
<div id="map" class="information"></div>

在我的地图点击事件中,特征值什么都没有。如何在点击它时获取特征值。

【问题讨论】:

  • 请先修复编译错误,太多了!您的格式也已关闭,可能是导致某些错误的原因。 overlay 未定义,map 的 props 相乘,hdms 未定义,++i 应为 i++displayFeatureInfo 在单击侦听器中定义,过早结束并且永远不会被调用。 content 未定义,coordinate 未定义
  • 我没有收到任何编译错误。map.forEachFeatureAtPixel(evt, function(feature, one)
  • 我将您的代码转储转换为截断的可执行文件。查看并修复错误。

标签: javascript openlayers openlayers-3 geoserver gwt-openlayers


【解决方案1】:

您正在为您的用例研究错误的示例,您需要getFeatureInfo call,因为您正在使用 Web 地图服务 (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;';
          }
        });
      }
    });

【讨论】:

    【解决方案2】:

    你可以像这样获取特征信息:

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

    示例代码:

    var vectorSource = new ol.source.Vector({
      format: new ol.format.GeoJSON(),
      url: function(extent) {
        return 'https://ahocevar.com/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.com/ 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.org/en/v4.6.5/build/ol.js"></script>
    <link href="https://openlayers.org/en/v4.6.5/css/ol.css" rel="stylesheet"/>
    <div id="map" class="map"  style="width: 98%; height: calc(95%); position: absolute;"></div>

    【讨论】:

    • 假设有一个匹配的 WFS 服务与他们正在查询的 WMS 一起使用 - 可能有,也可能没有。
    猜你喜欢
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    • 2011-12-04
    • 2016-05-19
    • 1970-01-01
    相关资源
    最近更新 更多