Javascript 从URL将GeoJSON加载到层中?
我正在使用Mapbox 2.1,并尝试从GeoJSON源代码构建叶绿素图,使用以下示例: 然而,我在第一个障碍上就失败了,因为我的GeoJSON源代码是一个纯GeoJSON文件,而不是像他们的示例那样的JS文件 所以这句话不适合我:Javascript 从URL将GeoJSON加载到层中?,javascript,leaflet,mapbox,Javascript,Leaflet,Mapbox,我正在使用Mapbox 2.1,并尝试从GeoJSON源代码构建叶绿素图,使用以下示例: 然而,我在第一个障碍上就失败了,因为我的GeoJSON源代码是一个纯GeoJSON文件,而不是像他们的示例那样的JS文件 所以这句话不适合我: var statesLayer = L.geoJson(statesData, { style: getStyle, onEachFeature: onEachFeature }).addTo(map); 他们将statesData定义为变量:
var statesLayer = L.geoJson(statesData, {
style: getStyle,
onEachFeature: onEachFeature
}).addTo(map);
他们将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)
});
您可以使用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)
});
谢谢,这就是我最后要做的。谢谢,这就是我最后要做的。谢谢,这就是我最后要做的。