简体   繁体   English

无法使用 openlayer 6 显示矢量图层

[英]can' t display a vector layer using openlayer 6

I am working on an openlayers map to add a vector layer with the source of the local geojson and gpx file in a Vuejs project, but the vector layer cannot be displayed.我正在处理一个openlayers地图以在Vuejs项目中添加带有本地geojson和gpx文件源的矢量图层,但无法显示矢量图层。 I tested outside of Vue.js and I have the same problem.我在 Vue.js 之外进行了测试,我遇到了同样的问题。

Voici le code :语音代码:

// classes required to display the map
import Map from 'ol/Map'
import View from 'ol/View'
import TileLayer from 'ol/layer/Tile'
import OSM from 'ol/source/OSM'

// Feature format for reading and writing data in the GPX format.
import GPX from 'ol/format/GPX'

// Feature format for reading and writing data in the GeoJSON format.
import GeoJSON from 'ol/format/GeoJSON'

// Provides a source of features for vector layers.
import VectorSource from 'ol/source/Vector'

// Vector data that is rendered client-side.
import VectorLayer from 'ol/layer/Vector'

// Openstreet Map Standard
const openStreetMapLayer = new TileLayer({
  source: new OSM(),
})

// Vector data source in GeoJSON format
const vectorGeoJSON = new VectorLayer({
  source: new VectorSource({
    url: 'data/pays.geojson',
    format: new GeoJSON()
  })
})

// Vector data source in GPX format
const vectorGPX = new VectorLayer({
  source: new VectorSource({
    url: 'data/capitales.gpx',
    format: new GPX()
  })
})

// declare the map 
new Map({
  layers: [openStreetMapLayer, vectorGPX, vectorGeoJSON],
  target: 'map',
  view: new View({
    center: [0, 0],
    zoom: 2
  })
})

for the geojson file receive this error:对于 geojson 文件收到此错误:

Uncaught SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at getObject (JSONFeature.js:197)
    at GeoJSON.JSONFeature.readFeatures (JSONFeature.js:53)
    at VectorSource.<anonymous> (featureloader.js:94)

and for the gpx file no error but nothing is displayed.并且对于 gpx 文件没有错误但没有显示任何内容。

I tried to add a style but the result remains the same.我尝试添加样式,但结果保持不变。

I created a simple example with parcel + openlayers reproducing the problem ici我创建了一个简单的例子,用parcel + openlayers 重现问题ici

I looked at the doc + the openlayers examples and I don't see what is causing the problem in my code?我查看了 doc + openlayers 示例,但没有看到是什么导致了我的代码中的问题?

yes i already tried to specify the full path.是的,我已经尝试指定完整路径。 I also renamed in .json and it doesn't work.我也在.json重命名,但它不起作用。 The code seems correct because I tried with the following code and it works.该代码似乎正确,因为我尝试使用以下代码并且它有效。 I do not understand why it does not work with the local file.我不明白为什么它不适用于本地文件。 Maybe you need to add a configuration in parcel or even webpack or vuejs?也许您需要在parcel甚至webpack或vuejs中添加配置?

this works :这有效:

// Vector data source in GeoJSON format
const vectorGeoJSON = new VectorLayer({
  source: new VectorSource({
    url: 'https://raw.githubusercontent.com/sandix34/Openlayers-test-workshop/master/data/pays.geojson',
    format: new GeoJSON()
  })
})

// Vector data source in GPX format
const vectorGPX = new VectorLayer({
  source: new VectorSource({
    url: 'https://raw.githubusercontent.com/sandix34/Openlayers-test-workshop/master/data/capitales.gpx',
    format: new GPX()
  })
})

Just copy the data folder and inside files into dist folder.只需将data文件夹和内部文件复制到dist文件夹中即可。

This problem happens because your application can't find the data folder.出现此问题是因为您的应用程序找不到data文件夹。 npm run start serve your application build ( dist folder) on localhost:1234 . npm run startlocalhost:1234上为您的应用程序构建( dist文件夹)提供服务。 The question is "Is there any data folder in localhost:1234 ?"问题是“localhost:1234 中是否有任何数据文件夹?” or "Can I access my data via localhost:1234/data ?".或“我可以通过 localhost:1234/data 访问我的数据吗?”。

To solve this problem as mention above you need to copy the whole data folder into the dist folder.要解决上面提到的这个问题,您需要将整个data文件夹复制到dist文件夹中。

In vue.js I moved the data folder to the public folder and the correct relative path is url: '../data/pays.geojson' .I deployed the application with netlify and it works.在 vue.js 中,我将data文件夹移动到public文件夹,正确的相对路径是url: '../data/pays.geojson' 。我用 netlify 部署了应用程序,它可以工作。 Thank you for your answers which helped me to find the solution.感谢您的回答,这帮助我找到了解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM