Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide GeoJSON loader function to prevent 403's #34

Merged
merged 1 commit into from
Aug 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,29 @@ const createInstance = ({ target, options }) => {
title = 'geojson', url, color, visible = true,
}) {
const style = styles(color);
const format = new GeoJSON();
const source = new VectorSource({
url,
format: new GeoJSON(),
format,
// Supply a loader function so we can be sure xhr.withCredentials === true.
loader(extent, resolution, featureProjection) {
const xhr = new XMLHttpRequest();
const onError = () => source.removeLoadedExtent(extent);
xhr.open('GET', url);
xhr.withCredentials = true;
xhr.onerror = onError;
xhr.onload = () => {
if (xhr.status === 200) {
const features = format.readFeatures(xhr.responseText, {
extent,
featureProjection,
});
source.addFeatures(features);
} else {
onError();
}
};
xhr.send();
},
});
const layer = new VectorLayer({
title,
Expand Down