Получить информацию о функции на карте нажмите в openlayers3 javascript - PullRequest
0 голосов
/ 03 мая 2018

я пытаюсь получить информацию об объекте на карте, используя openlayers 3 javascript с помощью примера 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.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>

на моем событии щелчка по карте значение функции - ничто. как получить значение функции при нажатии на нее.

Ответы [ 2 ]

0 голосов
/ 04 мая 2018

Вы изучаете неправильный пример для своего варианта использования, вам нужен вызов getFeatureInfo , так как вы используете WMS.

map.on('click', function(evt) {
      var url = wms_layer.getSource().getGetFeatureInfoUrl(
          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;';
          }
        });
      }
    });
0 голосов
/ 04 мая 2018

Вы можете получить информацию о функции, как это:

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>
...