如何在点击它时获取wkt多边形的所有信息?

时间:2018-02-21 13:25:40

标签: json sql-server openlayers openlayers-3 wkt

我以json格式从MSsql server 2012获取数据,我只需转换WKT字符串,以便使用ol.format.WKT()在地图上显示。

我想在弹出窗口中单击时显示多边形的ID和名称。 如何识别我点击哪个多边形?

我如何知道我点击多边形的地图并获取该多边形的数据?

for (var i = 0; i < geometries.length; i++) {

    var feature = wktReader.readFeature(geometries[i].GeomCol1.Geometry.WellKnownText);

    feature.getGeometry().transform('EPSG:4326', 'EPSG:3857');

    if (feature.getGeometry().getType() == 'Polygon') {
        feature.setStyle(new ol.style.Style({
            stroke: new ol.style.Stroke({
                color: 'blue',
                width: 1
            }),
            fill: new ol.style.Fill({
                color: 'rgba(0, 0, 255, 0.1)'
            })
       }));
       featureCollection.push(feature);
    }
}

这是我如何获得wkt string的一部分。

These are the polygons that i have shown, and i want to show a popup with the informations of the polygon which i click 这些是我显示的多边形,我想显示一个弹出窗口,其中包含我点击的多边形信息

This is a picture of how i have stored the spatial data in my MSsql server

这是我如何在我的MSsql服务器中存储空间数据的图片

由于

1 个答案:

答案 0 :(得分:0)

myQuery.AsEnumerable<Customers>() .MySelect2<IEnumerable<Customers>, Customers, String>(d => d.CustomerID).Dump(); api doc)可以获取某个位置的所有功能。不可否认,使用它可能会令人困惑。为每个图层调用传递给该函数的回调。如果回调返回truthy值,它将停止,然后 forEachFeatureAtPixel 返回您返回的回调。这使得选择特定功能变得非常完美。

&#13;
&#13;
forEachFeatureAtPixel
&#13;
var count = 2000;
var features = new Array(count);
var e = 4500000;
for (var i = 0; i < count; ++i) {
  var coordinates = [2 * e * Math.random() - e, 2 * e * Math.random() - e];
  features[i] = new ol.Feature({
    geometry: new ol.geom.Point(coordinates),
    myHappyAttribute: ('you are beautiful ' + i)
  });
}

var source = new ol.source.Vector({
  features: features
});

var layer = new ol.layer.Vector({source: source});


const view = new ol.View({
  center: [2500000, 0], zoom: 5
});
const map = new ol.Map({
  layers: [ 
    new ol.layer.Tile({
        source: new ol.source.OSM()
    }),
    layer
  ],
  target: 'map',
  view 
});


map.on('singleclick', function(evt) {
  const pixel = map.getEventPixel(evt.originalEvent);
  const feature = map.forEachFeatureAtPixel(
    pixel,
    someFeature => someFeature, // return first element
    {
      hitTolerance: 2 // how far off the click can be
    }
  );
  if (feature) {
    const attr = feature.get('myHappyAttribute');
    console.log('clicked on feature with attr:', attr);
  } else {
    console.log('not clicked on a feature');
  }
});
&#13;
&#13;
&#13;