| 1 |
;(function () { |
| 2 |
window.WPLeafletMapPlugin = window.WPLeafletMapPlugin || [] |
| 3 |
window.WPLeafletMapPlugin.unshift(initAjaxGeoJSON) |
| 4 |
|
| 5 |
function initAjaxGeoJSON() { |
| 6 |
L.AjaxGeoJSON = L.GeoJSON.extend({ |
| 7 |
options: { |
| 8 |
type: 'json', // 'json|kml|gpx' |
| 9 |
}, |
| 10 |
|
| 11 |
initialize: function (url, options) { |
| 12 |
L.setOptions(this, options) |
| 13 |
this._url = url |
| 14 |
this.layer = L.geoJson(null, this.options) |
| 15 |
}, |
| 16 |
|
| 17 |
onAdd: function (map) { |
| 18 |
var _this = this |
| 19 |
var type = this.options.type |
| 20 |
var xhr |
| 21 |
|
| 22 |
this.map = map |
| 23 |
|
| 24 |
map.addLayer(this.layer) |
| 25 |
|
| 26 |
if (!this.request) { |
| 27 |
this.request = xhr = new XMLHttpRequest() |
| 28 |
|
| 29 |
xhr.onreadystatechange = function () { |
| 30 |
var data |
| 31 |
if (xhr.readyState === xhr.DONE && xhr.status === 200) { |
| 32 |
if (type === 'json') { |
| 33 |
data = JSON.parse(xhr.responseText) |
| 34 |
} else if (['kml', 'gpx'].indexOf(type) !== -1) { |
| 35 |
data = window.toGeoJSON[type](xhr.responseXML) |
| 36 |
} |
| 37 |
_this.json = data |
| 38 |
_this.layer.addData(data) |
| 39 |
_this.fire('ready') |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
xhr.open('get', this._url, true) |
| 44 |
|
| 45 |
xhr.send() |
| 46 |
} |
| 47 |
this.fire('add') |
| 48 |
}, |
| 49 |
|
| 50 |
eachLayer: function (fnc) { |
| 51 |
this.layer.eachLayer(fnc) |
| 52 |
}, |
| 53 |
|
| 54 |
setStyle: function (style) { |
| 55 |
this.layer.setStyle(style) |
| 56 |
}, |
| 57 |
|
| 58 |
resetStyle: function (layer) { |
| 59 |
this.layer.resetStyle(layer) |
| 60 |
}, |
| 61 |
|
| 62 |
onRemove: function (map) { |
| 63 |
this.map.removeLayer(this.layer) |
| 64 |
}, |
| 65 |
|
| 66 |
getBounds: function () { |
| 67 |
return this.layer.getBounds() |
| 68 |
}, |
| 69 |
}) |
| 70 |
|
| 71 |
L.ajaxGeoJson = function (url, options) { |
| 72 |
return new L.AjaxGeoJSON(url, options) |
| 73 |
} |
| 74 |
} |
| 75 |
})() |
| 76 |
|