Javascript 在OpenLayers 3中使用编码多段线

Javascript 在OpenLayers 3中使用编码多段线,javascript,openlayers-3,Javascript,Openlayers 3,我在OpenLayers 3中尝试使用编码多段线时遇到问题。我有一个外部源提供编码字符串,但当我尝试将它们转换为特征并将它们添加到向量源时,我得到以下错误: Uncaught TypeError: Cannot read property 'ua' of undefined 以下是我正在使用的当前代码: vectorSource = new ol.source.Vector(); var layers = [ new ol.layer.Tile({ name: "SKL

我在OpenLayers 3中尝试使用编码多段线时遇到问题。我有一个外部源提供编码字符串,但当我尝试将它们转换为特征并将它们添加到向量源时,我得到以下错误:

Uncaught TypeError: Cannot read property 'ua' of undefined
以下是我正在使用的当前代码:

vectorSource = new ol.source.Vector();
var layers = [
    new ol.layer.Tile({
        name: "SKL Tile Server",
        source: new ol.source.OSM({
            url: "https://placeholder/osm/{z}/{x}/{y}.png",
            crossOrigin: null
        })
    }),
    new ol.layer.Vector({
        name: "polylines",
        source: vectorSource
    })
];

map = new ol.Map({
    layers: layers,
    target: 'report_map',
    view: new ol.View({
        center: ol.proj.transform(
            [-118.014670, 45.35724], 'EPSG:4326', 'EPSG:900913'),
        zoom: 10
    })
})

var addPolylineToMap = function (encoded_line, line_style) {

    var line = new ol.format.Polyline().readGeometry({
        source: encoded_line,
        options: {
            dataProjection: ol.proj.get('EPSG:4326'),
            featureProjection: ol.proj.get('EPSG:900913')
        }
    });
    line.setStyle(line_style);
    vectorSource.addFeature(new ol.Feature(line));
    return line;
};
诚然,我对OpenLayers 3还很陌生——但我已经进行了广泛的搜索,似乎找不到在OpenLayers 3中使用编码多段线的任何示例。

请尝试以下方法:

var addPolylineToMap = function (encoded_line, line_style) {

    var format = new ol.format.Polyline({
        //factor: 1e6
    });
    var line = format.readGeometry(encoded_line, {
        dataProjection: 'EPSG:4326',
        featureProjection: 'EPSG:900913'
    });

    var feature = new ol.Feature({geometry: line});
    feature.setStyle(line_style);
    vectorSource.addFeature(feature);

    return line;
};
试着这样做:

var addPolylineToMap = function (encoded_line, line_style) {

    var format = new ol.format.Polyline({
        //factor: 1e6
    });
    var line = format.readGeometry(encoded_line, {
        dataProjection: 'EPSG:4326',
        featureProjection: 'EPSG:900913'
    });

    var feature = new ol.Feature({geometry: line});
    feature.setStyle(line_style);
    vectorSource.addFeature(feature);

    return line;
};

谢谢,这似乎解决了这个问题。除了官方文档之外,你能推荐一些学习OpenLayers 3的资源吗?我不知道如何从我找到的资源中找到您的解决方案。我的来源是文档,每一个问题我都会阅读。啊,当然,检查所有这些,即使你不需要那个具体的解决方案。谢谢,这似乎解决了这个问题。除了官方文档之外,你能推荐一些学习OpenLayers 3的资源吗?我不知道如何从我找到的资源中找到您的解决方案。我的来源是文档,每一个问题我都会阅读。啊,当然还有,检查所有这些,即使你不需要那个特定的解决方案。