单击“打开图层”中的图层时的回调

时间:2019-06-07 21:03:05

标签: javascript openlayers-3

我创建了一个开放地图,其中包含带有多个多边形的矢量层。如何检测用户单击这些多边形并检测单击了哪个正方形?

      var map = new Map({
    layers: [
      new TileLayer({
        source: new TileJSON({
          url: 'https://maps.siemens.com/styles/osm-bright.json'
        })
      }),
      new VectorLayer({
        source: new VectorSource({
          features: polygonFeatures
        }),
        style: new Style({
          stroke: new Stroke({
            width: 1,
            color: [0, 0, 0, 1]
          }),
          fill: new Fill({
            color: [255, 0, 255, 0.3]
          })
        })
      })
    ],
    target: 'map',
    view: new View({
      center: midPoint,
      zoom: 6.1
    })
  });

这里是jsfiddle

1 个答案:

答案 0 :(得分:1)

在单击时,您可以按名称或其他属性来标识特征,如本示例Angular Support

命名多边形的最简单方法是使用minPoint和maxPoint。从范围创建多边形也更简单

  function mapSquare(minPoint, maxPoint) {
    var extent = minPoint.concat(maxPoint);
    var polygonFeature = new Feature({
      geometry: Polygon.fromExtent(minPoint.concat(maxPoint)).transform('EPSG:4326','EPSG:3857'),
      name: extent.toString()
    });
    return polygonFeature;
  }
相关问题