【问题标题】:Add GPX dynamically with openlayers使用 openlayers 动态添加 GPX
【发布时间】:2018-05-29 05:53:55
【问题描述】:

我试图通过从复选框中选择 gpx 文件来显示不同的路线,但矢量图层没有出现。

我以 OpenLayers 网页中的示例为指导:GPXDATADragandDrop 没有结果。

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 图层放在图层集合的顶部并自动显示,但是我有这个错误:“未捕获的异常:AssertionError:断言失败。有关详细信息,请参阅https://openlayers.org/en/v4.0.1/doc/errors/#41 。” 这意味着:需要一个 ol.style.Style 或一个 ol.style.Style 数组。

但是 var defaultStyle 已经是 Styles 的数组,所以我不明白错误。

谢谢。

【问题讨论】:

    标签: javascript openlayers-3


    【解决方案1】:

    您有 2 个地方可以在代码中设置样式

    • style: defaultStyle
    • style: styleFunction

    styleFunction 工作正常(它返回基于几何类型的样式)。 相反,defaultStyle 不起作用,因为它不是“ol.style.Style 或 ol.style.Style 的数组”。但它是一个对象。

    要解决此问题,您可以

    • style: defaultStyle 替换为style: styleFunction
    • 如果你的 GPX 只包含点,你可以使用style: defaultStyle['Point']
    • 如果它只包含行,你可以使用style: defaultStyle['LineString']

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多