使用openlayers动态添加GPX
Add GPX dynamically with openlayers
我试图通过从复选框中选择它们来显示来自 gpx 文件的不同路线,但是矢量图层没有出现。
我使用 OpenLayers 网页中的示例作为指导:GPXDATA and 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 图层放在图层集合的顶部并自动显示,但我却遇到了这个错误:"uncaught exception: AssertionError: Assertion failed. See https://openlayers.org/en/v4.0.1/doc/errors/#41 for details."
这意味着:预期 ol.style.Style 或 ol.style.Style.
的数组
但是var defaultStyle已经是一个Style数组了,所以没看懂错误
谢谢。
您在代码中有 2 个地方可以设置样式
style: defaultStyle
style: styleFunction
styleFunction
工作正常(它 returns 基于几何类型的样式)。
相反,defaultStyle
不起作用,因为它不是 "ol.style.Style or an array of ol.style.Style.",而是一个对象。
要解决此问题,您可以
- 将
style: defaultStyle
替换为 style: styleFunction
。
- 如果您的 GPX 只包含点,您可以使用
style: defaultStyle['Point']
- 如果只包含行,可以使用
style: defaultStyle['LineString']
我试图通过从复选框中选择它们来显示来自 gpx 文件的不同路线,但是矢量图层没有出现。
我使用 OpenLayers 网页中的示例作为指导:GPXDATA and 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 图层放在图层集合的顶部并自动显示,但我却遇到了这个错误:"uncaught exception: AssertionError: Assertion failed. See https://openlayers.org/en/v4.0.1/doc/errors/#41 for details." 这意味着:预期 ol.style.Style 或 ol.style.Style.
的数组但是var defaultStyle已经是一个Style数组了,所以没看懂错误
谢谢。
您在代码中有 2 个地方可以设置样式
style: defaultStyle
style: styleFunction
styleFunction
工作正常(它 returns 基于几何类型的样式)。
相反,defaultStyle
不起作用,因为它不是 "ol.style.Style or an array of ol.style.Style.",而是一个对象。
要解决此问题,您可以
- 将
style: defaultStyle
替换为style: styleFunction
。 - 如果您的 GPX 只包含点,您可以使用
style: defaultStyle['Point']
- 如果只包含行,可以使用
style: defaultStyle['LineString']