将 GeoJSON 从 URL?
Load GeoJSON into layer from a URL?
我正在使用 Mapbox 2.1,我正在尝试从 GeoJSON 源构建一个叶绿素地图,从这个例子开始工作:https://www.mapbox.com/mapbox.js/example/v1.0.0/choropleth/
然而,我在第一个障碍就倒下了,因为我的 GeoJSON 源是一个纯 GeoJSON 文件,而不是像他们的示例那样的 JS 文件。
所以这条线对我不起作用:
var statesLayer = L.geoJson(statesData, {
style: getStyle,
onEachFeature: onEachFeature
}).addTo(map);
他们的JS file将statesData
定义为一个变量:
var statesData = { "type": "FeatureCollection" ... };
但是我的 GeoJSON 文件只是一个 GeoJSON 文件。
如何定义 statesData
的等效项以便我可以按照示例进行操作?
我想我可以使用 eval
来读取 GeoJSON,但这通常不是一个好主意。
您可以使用 ajax 从您自己的站点获取数据
这里有一个 jquery 的方法。
$.ajax({
dataType: "json", //you shouldn't need to declare geojson
url: "data/district.geojson",
success: function(data) {
//whatever you want to do with the data, alternatively you could use a closer around the ajax request to capture the data into a variable depending on how you have your code organized
}
}).error(function(e) {
console.log(e)
});
我正在使用 Mapbox 2.1,我正在尝试从 GeoJSON 源构建一个叶绿素地图,从这个例子开始工作:https://www.mapbox.com/mapbox.js/example/v1.0.0/choropleth/
然而,我在第一个障碍就倒下了,因为我的 GeoJSON 源是一个纯 GeoJSON 文件,而不是像他们的示例那样的 JS 文件。
所以这条线对我不起作用:
var statesLayer = L.geoJson(statesData, {
style: getStyle,
onEachFeature: onEachFeature
}).addTo(map);
他们的JS file将statesData
定义为一个变量:
var statesData = { "type": "FeatureCollection" ... };
但是我的 GeoJSON 文件只是一个 GeoJSON 文件。
如何定义 statesData
的等效项以便我可以按照示例进行操作?
我想我可以使用 eval
来读取 GeoJSON,但这通常不是一个好主意。
您可以使用 ajax 从您自己的站点获取数据
这里有一个 jquery 的方法。
$.ajax({
dataType: "json", //you shouldn't need to declare geojson
url: "data/district.geojson",
success: function(data) {
//whatever you want to do with the data, alternatively you could use a closer around the ajax request to capture the data into a variable depending on how you have your code organized
}
}).error(function(e) {
console.log(e)
});