Access to Image from origin 'null' has been blocked by CORS policy
You're running into a CORS error.
Trying to access your file using the local file system doesn't work in your case.
Origin
is null because it's your local file system. Could you possibly host this png file?
Suggestion:
Host these files to an AWS S3 bucket instead. Then you can use the http
protocol rather than the file
protocol. OR setup some http server on your local system and use http
to your localhost
to serve the files from if you want to keep everything local.
More Reading:
How CORS Works
The problem was actually solved by providing crossOrigin: null to OpenLayers OSM source:
var newLayer = new ol.layer.Tile({
source: new ol.source.OSM({
url: 'E:/Maperitive/Tiles/vychod/{z}/{x}/{y}.png',
crossOrigin: null
})
});
Under the covers there will be some form of URL loading request. You can't load images or any other content via this method from a local file system.
Your image needs to be loaded via a web server, so accessed via a proper http URL.
To solve your error I propose this solution: to work on Visual studio code editor and install live server extension in the editor, which allows you to connect to your local server, for me I put the picture in my workspace 127.0.0.1:5500/workspace/data/pict.png and it works!
Try to bypass CORS:
For Chrome: edit shortcut or with cmd: C:\Chrome.exe --disable-web-security
For Firefox: Open Firefox and type about:config into the URL bar. search for: security.fileuri.strict_origin_policy set to false