简介:
leaflet-ajax
Allows you to call JSON via an Ajax call with a jsonp fallback.
允许您通过带有jsonp回退的Ajax调用调用JSON。
var geojsonLayer = new L.GeoJSON.AJAX("geojson.json");
for jsonp add the option "dataType" and set it to "jsonp"
对于jsonp,添加选项“dataType”并将其设置为“jsonp”
var geojsonLayer = L.geoJson.ajax("http:webhost.fake/geojson.jsonp",{dataType:"jsonp"});
Note that data starts to download when the layer is created NOT when it’s added to the map in order to get a head start.
请注意,数据在图层创建时开始下载,而不是将其添加到地图中以获得领先优势时开始下载。
You may pass either a url string or an array of url strings if you want to download multiple things (handy if your downloading data from an ESRI based thing which will have separate line, point, and poly features).
如果您想下载多个内容,您可以传递一个url字符串或一个url字符串数组(如果您从一个基于ESRI的东西下载数据,它将具有单独的线、点和多边形特性),那么您可以传递一个url字符串或一个url字符串数组。
As you see you can also use lower case methods without creating new objects
如您所见,您也可以使用小写方法而不创建新对象
For weirder jsonp you can set "callbackParam" for if you need to change the name of the callback parameter to something besides "callback", e.g. Mapquest Nominative Open uses "json_callback" instead of "callback".
对于更奇怪的jsonp,如果需要将回调参数的名称改为“callbackParam”,例如Mapquest nomingive Open使用“json_callback”而不是“callback”。
If you want to be able to load stuff from the file system (with appropriate custom flags set) set local to true.
如果希望能够从文件系统加载内容(设置适当的自定义标志),请将local设置为true。
If you want to set headers to the XMLHttpRequest set the 'headers' option equal to an object.
如果要将headers设置为XMLHttpRequest,请将“headers”选项设置为等于一个对象。
Gives off three events data:loading
, data:progress
and data:loaded
.
发出三个事件数据:加载, 数据:进度和数据:已加载。
data:loading
fires before we start downloading things, note if the constructor is given a url it won't wait to be added to the map to start downloading the data, but it does do an async wait so you have time to add a listener to it (and so leaflet.spin will work with it).data:progress
is called each time a file is downloaded and passes the downloaded geojson as event data.data:loaded
is called when all files have downloaded, this is mainly different fromdata:progress
when you are downloading multiple things.
You can also add a middleware function which is called after you download the data but before you add it to leaflet:
您还可以添加一个中间件函数,该函数在下载数据后添加到活页之前调用:
var geojsonLayer = L.geoJson.ajax("route/to/esri.json",{
middleware:function(data){
return esri2geoOrSomething(json);
}
});
addUrl does not clear the current layers but adds to the current one, e.g.:
addUrl不清除当前层,而是添加到当前层,例如:
var geojsonLayer = L.geoJson.ajax("data.json");
geojsonLayer.addUrl("data2.json");//we now have 2 layers
geojsonLayer.refresh();//redownload the most recent layer
geojsonLayer.refresh("new1.json");//add a new layer replacing whatever is there
last but now least we can refilter layers without re adding them
最后,但现在至少我们可以重新过滤层而不需要重新添加它们
var geojsonLayer = L.geoJson.ajax("data.json");
geojsonLayer.refilter(function(feature){
return feature.properties.key === values;
});
Behind the scenes are two new classes L.Util.ajax = function (url) for same origin requests and L.Util.jsonp = function (url,options) cross origin ones. Both return promises, which have an additional abort method that will abort the ajax request.
幕后是两个新的类L.Util.ajax=function(url)用于同一源请求和L.Util.jsonp=function(url,options)跨域。附加的ajax请求都将有一个abort请求。
L.Util.ajax("url/same/origin.xml").then(function(data){
doStuff(data);
});
//or
L.Util.jsonp("http://www.dif.ori/gin").then(function(data){
doStuff(data);
});
In related news L.Util.Promise
is now a constructor for a promise, it takes one argument, a resolver function.
Some of the jsonp code inspired by/taken from this interesting looking plugin that I have failed to make heads nor tails of (the plugin, not the jsonp code)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)