在 OpenLayers 3 中使用编码折线
Using encoded polylines in 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 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;
};
我在 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;
};