OpenLayers 6:示例框选择错误

严格按照以下openlayers网站的示例进行操作:

https://openlayers.org/en/latest/examples/box-selection.html?q=feature

我在本地遇到以下错误:

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)

我认为该错误来自尝试在本地加载geojson文件。

var vectorSource = new VectorSource ({
  url: 'data/geojson/countries.geojson',  format: new GeoJSON ()
});

countries.geojson: https://r68tk.csb.app/data/geojson/countries.geojson

OpenLayers 6:示例框选择错误

如何解决?

wanglili19860813 回答:OpenLayers 6:示例框选择错误

我找到了解决方法。

不幸的是,在OpenLayers中开发应用程序的过程中,由于 CORS ,在本地加载文件(geojson,png等)时出现了问题。

问题: enter image description here

关于here主题的精彩文章。

为解决此问题,我使用了名为“ moesif CORS extension”的Chrome扩展程序。

启用它后,我可以轻松加载.geojson文件。

成功了! enter image description here

还有更多方法可以在此地址为您的开发环境停止CORS:https://medium.com/@dtkatz/3-ways-to-fix-the-cors-error-and-how-access-control-allow -origin-works-d97d55946d9

我希望帮助某人获得此信息!

本文链接:https://www.f2er.com/3126757.html

大家都在问