leaflet-ajax

简介:

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:loadingdata: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 from data: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)

https://github.com/calvinmetcalf/leaflet-ajax

原文地址:https://www.cnblogs.com/2008nmj/p/14171968.html