我正在尝试自定义此示例:
http://openlayers.org/en/latest/examples/feature-move-animation.html
var strGeoJson = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"LineString","coordinates":[[25.094146728515625,57.51877294160811],[25.135345458984375,57.55857562213471],[25.11749267578125,57.583614274541404],[25.022735595703125,57.58508660014084],[25.017242431640625,57.633639928856965],[25.11199951171875,57.655688188735766],[25.081787109374996,57.69240553526455],[24.97055053710937,57.68873547372526]]}}]}';
var route = (
new ol.format.Polyline({
factor: 1e6
}).readFeatures(
strGeoJson,
{
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
}
)
);
var routeFeature = new ol.Feature({
type: 'route',
geometry: route
});
vectorLayerSource = new ol.source.Vector({
features: [routeFeature]
});
但是所有的时间:
ol.js?20180608153258:47 Uncaught TypeError: a.addEventListener is not a function
at y (ol.js?20180608153258:47)
at Hk.k.Oe (ol.js?20180608153258:302)
at Hk.b (ol.js?20180608153258:46)
at Hk.Sc.b (ol.js?20180608153258:49)
at Yc (ol.js?20180608153258:51)
at Hk.k.set (ol.js?20180608153258:51)
at Hk.k.H (ol.js?20180608153258:52)
at new Hk (ol.js?20180608153258:301)
at HTMLDocument.<anonymous> (history-map:1372)
at i (jquery-2.2.3.min.js:2)
at Object.fireWith [as resolveWith] (jquery-2.2.3.min.js:2)
at Function.ready (jquery-2.2.3.min.js:2)
at HTMLDocument.J (jquery-2.2.3.min.js:2)
有人能说出问题出在哪里吗?如何从geojson画一条线。
答案 0 :(得分:2)
您的主要问题是您使用的是ol.format.Polyline
,而您应该使用ol.format.GeoJSON
。
提示是关于以下代码
var route = /** @type {ol.geom.LineString} */ (new ol.format.GeoJSON().readFeature(JSON.parse(strGeoJson).features[0], {
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
})).getGeometry();
JSON.parse(strGeoJson).features[0]
为您提供GeoJSON功能。然后你解析它,然后从中得到ol.geom.LineString
(getGeometry()
)
您可以在代码中看到a working solution(我已经更改了中心以适合您的样本以及使用OpenStreetMap而不是Bing的背景地图)