我试图通过从复选框中选择gpx文件来显示不同的路线,但矢量图层不会出现。
我使用OpenLayers网页中的示例作为指南:GPXDATA和DragandDrop没有结果。
Main.php
<!DOCTYPE html>
<html>
<head>
<title>Mapa simple de OpenStreetMap con Open Layers</title>
<link rel="stylesheet" href="https://openlayers.org/en/v4.0.1/css/ol.css" type="text/css">
<script src="https://openlayers.org/en/v4.0.1/build/ol.js"></script>
<link rel="stylesheet" href="css/main.css" type="text/css">
<script src="js/main.js"></script>
</head>
<body>
<h1 id="maintitle">Rutas de Limpieza</h1>
<div id="content">
<div id="menu">
<ul>
<li>Mostrar Rutas</li>
<ul>Rutas de Barredoras
<li><input id="r1" name="ba_r1" type="checkbox">Ruta B1</li>
<li><input name="ba_r2" type="checkbox">Ruta B2</li>
<li><input name="ba_r3" type="checkbox">Ruta B3</li>
</ul>
</ul>
<button id="sr_btn">Mostrar Rutas Seleccionas</button>
</div>
<div id="map" class="map"></div>
</div>
</body>
</html>
main.js
window.onload = function(){
var defaultStyle = {
'Point': new ol.style.Style({
image: new ol.style.Circle({
fill: new ol.style.Fill({
color: 'rgba(255,255,0,0.5)'
}),
radius: 5,
stroke: new ol.style.Stroke({
color: '#ff0',
width: 1
})
})
}),
'LineString': new ol.style.Style({
stroke: new ol.style.Stroke({
color: '#f00',
width: 3
})
}),
'Polygon': new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(0,255,255,0.5)'
}),
stroke: new ol.style.Stroke({
color: '#0ff',
width: 1
})
}),
'MultiPoint': new ol.style.Style({
image: new ol.style.Circle({
fill: new ol.style.Fill({
color: 'rgba(255,0,255,0.5)'
}),
radius: 5,
stroke: new ol.style.Stroke({
color: '#f0f',
width: 1
})
})
}),
'MultiLineString': new ol.style.Style({
stroke: new ol.style.Stroke({
color: '#00f',
width: 3
})
}),
'MultiPolygon': new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(25,120,255,0.5)'
}),
stroke: new ol.style.Stroke({
color: '#00f',
width: 1
})
})
};
var styleFunction = function(feature, resolution) {
var featureStyleFunction = feature.getStyleFunction();
if (featureStyleFunction) {
return featureStyleFunction.call(feature, resolution);
} else {
return defaultStyle[feature.getGeometry().getType()];
}
};
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
target: 'map',
view: new ol.View({
center: ol.proj.fromLonLat([2.5, 39.5]),
zoom: 12
})
});
var btn_sr = this.document.getElementById("sr_btn").addEventListener('click', function(){
var vector1 = new ol.layer.Vector({
source: new ol.source.Vector({
url: 'gpx/qq2.gpx',
format: new ol.format.GPX()
}),
style: defaultStyle
});
map.addLayer(new ol.layer.Vector({
renderMode: 'image',
source: vector1,
style: styleFunction
}));
});
}
对于我可以从API理解的内容,map.addLayer函数应该将vector1图层放在图层集合的顶部并自动显示,但我有这样的错误:&#34;未捕获的异常:AssertionError:断言失败。有关详细信息,请参阅https://openlayers.org/en/v4.0.1/doc/errors/#41。&#34; 这意味着:期望ol.style.Style或ol.style.Style数组。
但var defaultStyle已经是一个样式数组,所以我不理解错误。
感谢。
答案 0 :(得分:0)
您有2个地方可以在代码中设置样式
java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.
style: defaultStyle
style: styleFunction
正常工作(它返回基于几何类型的样式)。
相反,styleFunction
无效,因为它不是&#34; ol.style.Style或ol.style.Style的数组。&#34;但它是一个对象。
要解决此问题,您可以
defaultStyle
替换为style: defaultStyle
。style: styleFunction
style: defaultStyle['Point']