[英]Callback on clicking a layer in Open Layer
I have an Open Map that I created, with a vector layer with several polygons. 我创建了一个开放地图,其中包含带有多个多边形的矢量层。 How can I detect a user click on these polygons, and detect which square was clicked?
如何检测用户单击这些多边形,并检测单击了哪个正方形?
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
})
});
You can identify features by name or other property when clicking as in this example https://openlayers.org/en/latest/examples/vector-layer.html 您可以在单击时按名称或其他属性来标识要素,例如本示例中的https://openlayers.org/en/latest/examples/vector-layer.html
The easiest way to name your polygons would be to use the minPoint and maxPoint. 命名多边形的最简单方法是使用minPoint和maxPoint。 It is also simpler to create a polygon from an extent
从范围创建多边形也更简单
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.