严格按照以下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
如何解决?
答案 0 :(得分:0)
我找到了解决方法。
不幸的是,在OpenLayers中开发应用程序的过程中,由于 CORS ,在本地加载文件(geojson,png等)时出现了问题。
关于here主题的精彩文章。
为解决此问题,我使用了名为“ moesif CORS extension”的Chrome扩展程序。
启用它后,我可以轻松加载.geojson文件。
还有更多方法可以在此地址为您的开发环境停止CORS:https://medium.com/@dtkatz/3-ways-to-fix-the-cors-error-and-how-access-control-allow -origin-works-d97d55946d9
我希望帮助某人获得此信息!