簡體   English   中英

從geojson繪制openlayers折線

[英]Draw openlayers polyline from geojson

我正在嘗試自定義此示例:

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畫一條線。

您的主要問題是使用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()

您可以使用代碼看到一個可行的解決方案 (我已經使用OpenStreetMap而不是Bing更改了中心以適合您的示例和背景圖)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM