我试图通过自定义样式在openlayers 3中显示选定要素上的顶点。我已经为多边形做到了这一点,但是在所有要素类型(点,线多边形)上都需要动态地实现它
多边形的solution使用多种样式,如下所示。
var styleFunction = function() {
return [
new ol.style.Style({
image: new ol.style.Circle({
radius: 5,
fill: new ol.style.Fill({
color: '#00aaff'
}),
stroke: new ol.style.Stroke({color: '#00aaff', width: 2})
}),
geometry: function(feature) {
var coordinates = feature.getGeometry().getCoordinates()[0];
return new ol.geom.MultiPoint(coordinates);
}
}),
new ol.style.Style({
stroke: new ol.style.Stroke({
color: "#00aaff",
width: 3
}),
fill: new ol.style.Fill({
color: "rgba(255,255,255,0.4)"
})
})
]
}
该解决方案适用于多边形,但是当选择线时,顶点不显示,并且当选择点时,我的styleFunction会完全断开。
我在点上出现以下错误:
TypeError: e is undefined SimpleGeometry.js:196
setLayout SimpleGeometry.js:196
setCoordinates MultiPoint.js:158
e MultiPoint.js:25
geometry (index):128
Ua vector.js:128
Ua vector.js:127
renderFeature VectorLayer.js:404
E VectorLayer.js:353
<anonymous> (index):975
prepareFrame VectorLayer.js:370
renderFrame Map.js:159
renderFrame_ PluggableMap.js:1232
animationDelay_ PluggableMap.js:191
<anonymous> (index):975
有人可以帮助我以支持所有功能类型的方式修改styleFunction吗?
感谢任何潜在客户
答案 0 :(得分:0)
getCoordinates
根据传递的几何类型返回不同的坐标对象,从而对每种几何类型进行不同的处理。
检查一下
var styleFunction = function() {
return [
new ol.style.Style({
image: new ol.style.Circle({
radius: 5,
fill: new ol.style.Fill({
color: '#00aaff'
}),
stroke: new ol.style.Stroke({color: '#00aaff', width: 2})
}),
geometry: function(feature) {
var coordinates;
if (feature.getGeometry().getType() == "LineString"){
coordinates = feature.getGeometry().getCoordinates();
} else if (feature.getGeometry().getType() == "Polygon"){
coordinates = feature.getGeometry().getCoordinates()[0];
} else if (feature.getGeometry().getType() == "Pont"){
//not 100% sure this would work!!!!
coordinates = [feature.getGeometry().getCoordinates()];
} else {
alert("maybe you need to handle also multi geometries");
}
return new ol.geom.MultiPoint(coordinates);
}
}),
new ol.style.Style({
stroke: new ol.style.Stroke({
color: "#00aaff",
width: 3
}),
fill: new ol.style.Fill({
color: "rgba(255,255,255,0.4)"
})
})
]
}