Leaflet variable for import data geojson
up vote
2
down vote
favorite
I just used a leaflet. From the geoJSON demo page I saw if you want to include data you have to use
<script src="data/us-states.geojson"></script>
and if you open the files looks like
var ustates = {
"type": "FeatureCollection",
......
};
and if you want to call them use
var data = [ustates] ;
Is there another way to call the data?
The geojson file that I have has no initial variable and looks like this:
{
"type": "FeatureCollection",
......
}
I have lots of data and I have to open 1 by 1 to add variable on geojson data so I mean can I just call the data like
var ustates = <?php include "data/us-states.geojson"; ?>
var data = [ustates];
leaflet javascript geojson
add a comment |
up vote
2
down vote
favorite
I just used a leaflet. From the geoJSON demo page I saw if you want to include data you have to use
<script src="data/us-states.geojson"></script>
and if you open the files looks like
var ustates = {
"type": "FeatureCollection",
......
};
and if you want to call them use
var data = [ustates] ;
Is there another way to call the data?
The geojson file that I have has no initial variable and looks like this:
{
"type": "FeatureCollection",
......
}
I have lots of data and I have to open 1 by 1 to add variable on geojson data so I mean can I just call the data like
var ustates = <?php include "data/us-states.geojson"; ?>
var data = [ustates];
leaflet javascript geojson
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I just used a leaflet. From the geoJSON demo page I saw if you want to include data you have to use
<script src="data/us-states.geojson"></script>
and if you open the files looks like
var ustates = {
"type": "FeatureCollection",
......
};
and if you want to call them use
var data = [ustates] ;
Is there another way to call the data?
The geojson file that I have has no initial variable and looks like this:
{
"type": "FeatureCollection",
......
}
I have lots of data and I have to open 1 by 1 to add variable on geojson data so I mean can I just call the data like
var ustates = <?php include "data/us-states.geojson"; ?>
var data = [ustates];
leaflet javascript geojson
I just used a leaflet. From the geoJSON demo page I saw if you want to include data you have to use
<script src="data/us-states.geojson"></script>
and if you open the files looks like
var ustates = {
"type": "FeatureCollection",
......
};
and if you want to call them use
var data = [ustates] ;
Is there another way to call the data?
The geojson file that I have has no initial variable and looks like this:
{
"type": "FeatureCollection",
......
}
I have lots of data and I have to open 1 by 1 to add variable on geojson data so I mean can I just call the data like
var ustates = <?php include "data/us-states.geojson"; ?>
var data = [ustates];
leaflet javascript geojson
leaflet javascript geojson
edited 16 hours ago
Stephen Lead
14k1278181
14k1278181
asked 18 hours ago
AdityaDS
1114
1114
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
5
down vote
We use this function in our project (credits go to Roberto MF):
function fetchJSON(url) {
return fetch(url)
.then(function(response) {
return response.json();
});
}
It uses the Fetch API to download the file. You can simply use your path data/us-states.geojson
as url
. Example with your data:
var data = fetchJSON('data/us-states.geojson');
Be aware that this doesn't work in Internet Explorer by default, but you can polyfill it by adding this line to your HTML-<head>
:
<script src="https://bowercdn.net/c/fetch-1.0.0/fetch.js" integrity="sha384-j9GCh0V617Ks+uEOZnAhwzTOWu5lPIlPW3QYRSfEwXd+x7VqP1XHNLgj3AIX7Mo0" crossorigin="anonymous"></script>
add a comment |
up vote
1
down vote
Very similar to the previous answer we use XHR to get the file and load the data into Leaflet.
const xhr = new XMLHttpRequest();
xhr.open('GET', 'data/us-states.geojson');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.responseType = 'json';
xhr.onload = function() {
if (xhr.status !== 200) return
L.geoJSON(xhr.response).addTo(map);
};
xhr.send();
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
We use this function in our project (credits go to Roberto MF):
function fetchJSON(url) {
return fetch(url)
.then(function(response) {
return response.json();
});
}
It uses the Fetch API to download the file. You can simply use your path data/us-states.geojson
as url
. Example with your data:
var data = fetchJSON('data/us-states.geojson');
Be aware that this doesn't work in Internet Explorer by default, but you can polyfill it by adding this line to your HTML-<head>
:
<script src="https://bowercdn.net/c/fetch-1.0.0/fetch.js" integrity="sha384-j9GCh0V617Ks+uEOZnAhwzTOWu5lPIlPW3QYRSfEwXd+x7VqP1XHNLgj3AIX7Mo0" crossorigin="anonymous"></script>
add a comment |
up vote
5
down vote
We use this function in our project (credits go to Roberto MF):
function fetchJSON(url) {
return fetch(url)
.then(function(response) {
return response.json();
});
}
It uses the Fetch API to download the file. You can simply use your path data/us-states.geojson
as url
. Example with your data:
var data = fetchJSON('data/us-states.geojson');
Be aware that this doesn't work in Internet Explorer by default, but you can polyfill it by adding this line to your HTML-<head>
:
<script src="https://bowercdn.net/c/fetch-1.0.0/fetch.js" integrity="sha384-j9GCh0V617Ks+uEOZnAhwzTOWu5lPIlPW3QYRSfEwXd+x7VqP1XHNLgj3AIX7Mo0" crossorigin="anonymous"></script>
add a comment |
up vote
5
down vote
up vote
5
down vote
We use this function in our project (credits go to Roberto MF):
function fetchJSON(url) {
return fetch(url)
.then(function(response) {
return response.json();
});
}
It uses the Fetch API to download the file. You can simply use your path data/us-states.geojson
as url
. Example with your data:
var data = fetchJSON('data/us-states.geojson');
Be aware that this doesn't work in Internet Explorer by default, but you can polyfill it by adding this line to your HTML-<head>
:
<script src="https://bowercdn.net/c/fetch-1.0.0/fetch.js" integrity="sha384-j9GCh0V617Ks+uEOZnAhwzTOWu5lPIlPW3QYRSfEwXd+x7VqP1XHNLgj3AIX7Mo0" crossorigin="anonymous"></script>
We use this function in our project (credits go to Roberto MF):
function fetchJSON(url) {
return fetch(url)
.then(function(response) {
return response.json();
});
}
It uses the Fetch API to download the file. You can simply use your path data/us-states.geojson
as url
. Example with your data:
var data = fetchJSON('data/us-states.geojson');
Be aware that this doesn't work in Internet Explorer by default, but you can polyfill it by adding this line to your HTML-<head>
:
<script src="https://bowercdn.net/c/fetch-1.0.0/fetch.js" integrity="sha384-j9GCh0V617Ks+uEOZnAhwzTOWu5lPIlPW3QYRSfEwXd+x7VqP1XHNLgj3AIX7Mo0" crossorigin="anonymous"></script>
edited 17 hours ago
answered 18 hours ago
Stefan
1,025118
1,025118
add a comment |
add a comment |
up vote
1
down vote
Very similar to the previous answer we use XHR to get the file and load the data into Leaflet.
const xhr = new XMLHttpRequest();
xhr.open('GET', 'data/us-states.geojson');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.responseType = 'json';
xhr.onload = function() {
if (xhr.status !== 200) return
L.geoJSON(xhr.response).addTo(map);
};
xhr.send();
add a comment |
up vote
1
down vote
Very similar to the previous answer we use XHR to get the file and load the data into Leaflet.
const xhr = new XMLHttpRequest();
xhr.open('GET', 'data/us-states.geojson');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.responseType = 'json';
xhr.onload = function() {
if (xhr.status !== 200) return
L.geoJSON(xhr.response).addTo(map);
};
xhr.send();
add a comment |
up vote
1
down vote
up vote
1
down vote
Very similar to the previous answer we use XHR to get the file and load the data into Leaflet.
const xhr = new XMLHttpRequest();
xhr.open('GET', 'data/us-states.geojson');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.responseType = 'json';
xhr.onload = function() {
if (xhr.status !== 200) return
L.geoJSON(xhr.response).addTo(map);
};
xhr.send();
Very similar to the previous answer we use XHR to get the file and load the data into Leaflet.
const xhr = new XMLHttpRequest();
xhr.open('GET', 'data/us-states.geojson');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.responseType = 'json';
xhr.onload = function() {
if (xhr.status !== 200) return
L.geoJSON(xhr.response).addTo(map);
};
xhr.send();
answered 13 hours ago
Dennis Bauszus
1,25021019
1,25021019
add a comment |
add a comment |
Thanks for contributing an answer to Geographic Information Systems Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f305646%2fleaflet-variable-for-import-data-geojson%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown