# leaflet-map/2.16.2/scripts/leaflet-ajax-geojson.js

Leaflet Map, version 2.16.2. 70 lines.

- Page: https://pluginprobe.com/plugins/leaflet-map/2.16.2/code/scripts/leaflet-ajax-geojson.js
- Raw: https://pluginprobe.com/plugins/leaflet-map/2.16.2/raw/scripts/leaflet-ajax-geojson.js
- Modified: 2017-11-20T23:03:44+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/leaflet-map/2.16.2/code/scripts/leaflet-ajax-geojson.js#L10-L20`.

```javascript
L.AjaxGeoJSON = L.GeoJSON.extend({
    options : {
        type: 'json' // 'json|kml|gpx'
    },

    initialize : function (url, options) {
        L.setOptions(this, options);
        this._url = url;
        this.layer = L.geoJson(null, this.options);
    },

    onAdd : function (map) {
        var _this = this,
            type = this.options.type,
            xhr;

        this.map = map;

        map.addLayer( this.layer );

        if (!this.request) {
            this.request = xhr = new XMLHttpRequest();

            xhr.onreadystatechange = function () {
                var data;
                if (xhr.readyState === xhr.DONE &&
                    xhr.status === 200) {
                    if (type === 'json') {
                        data = JSON.parse( xhr.responseText );
                    } else if (['kml', 'gpx'].indexOf(type) !== -1) {
                        data = window.toGeoJSON[ type ]( xhr.responseXML );
                    }
                    _this.json = data;
                    _this.layer.addData( data );
                    _this.fire('ready');
                }
            };

            xhr.open('get', this._url, true);

            xhr.send();
        }
        this.fire('add');
    },

    eachLayer : function (fnc) {
        this.layer.eachLayer( fnc );
    },

    setStyle : function (style) {
        
        this.layer.setStyle( style );
    },

    resetStyle : function (layer) {
        this.layer.resetStyle( layer );
    },

    onRemove : function (map) {
        this.map.removeLayer( this.layer );        
    },

    getBounds : function () {
        return this.layer.getBounds();
    }
});

L.ajaxGeoJson = function( url, options ) {
    return new L.AjaxGeoJSON( url, options );
};
```
