GeoJSON

Represents a GeoJSON object or an array of GeoJSON objects. Allows you to parse GeoJSON data and display it on the map. Extends FeatureGroup.

Usage example

L.geoJSON(data, {
    style: function (feature) {
        return {color: feature.properties.color};
    }
}).bindPopup(function (layer) {
    return layer.feature.properties.description;
}).addTo(map);

Creation

Factory Description
L.geoJSON(<Object> geojson?, <GeoJSON options> options?) Creates a GeoJSON layer. Optionally accepts an object in GeoJSON format to display on the map (you can alternatively add it later with addData method) and an options object.

Options

Option Type Default Description
pointToLayer Function * A Function defining how GeoJSON points spawn Leaflet layers. It is internally called when data is added, passing the GeoJSON point feature and its LatLng. The default is to spawn a default Marker:
function(geoJsonPoint, latlng) {
    return L.marker(latlng);
}
style Function * A Function defining the Path options for styling GeoJSON lines and polygons, called internally when data is added. The default value is to not override any defaults:
function (geoJsonFeature) {
    return {}
}
onEachFeature Function * A Function that will be called once for each created Feature, after it has been created and styled. Useful for attaching events and popups to features. The default is to do nothing with the newly created layers:
function (feature, layer) {}
filter Function * A Function that will be used to decide whether to show a feature or not. The default is to show all features:
function (geoJsonFeature) {
    return true;
}
coordsToLatLng Function * A Function that will be used for converting GeoJSON coordinates to LatLngs. The default is the coordsToLatLng static method.
Option Type Default Description
pane String 'overlayPane' By default the layer will be added to the map's overlay pane. Overriding this option will cause the layer to be placed on another pane by default.

Events

Event Data Description
layeradd LayerEvent Fired when a layer is added to this FeatureGroup
layerremove LayerEvent Fired when a layer is removed from this FeatureGroup
Event Data Description
add Event Fired after the layer is added to a map
remove Event Fired after the layer is removed from a map
Event Data Description
popupopen PopupEvent Fired when a popup bound to this layer is opened
popupclose PopupEvent Fired when a popup bound to this layer is closed
Event Data Description
tooltipopen TooltipEvent Fired when a tooltip bound to this layer is opened.
tooltipclose TooltipEvent Fired when a tooltip bound to this layer is closed.

Methods

Method Returns Description
setStyle(<Path options> style) this

Sets the given path options to each layer of the group that has a setStyle method.

bringToFront() this

Brings the layer group to the top of all other layers

bringToBack() this

Brings the layer group to the back of all other layers

getBounds() LatLngBounds

Returns the LatLngBounds of the Feature Group (created from bounds and coordinates of its children).

Method Returns Description
toGeoJSON() Object

Returns a GeoJSON representation of the layer group (as a GeoJSON GeometryCollection).

addLayer(<Layer> layer) this

Adds the given layer to the group.

removeLayer(<Layer> layer) this

Removes the given layer from the group.

removeLayer(<Number> id) this

Removes the layer with the given internal ID from the group.

hasLayer(<Layer> layer) Boolean

Returns true if the given layer is currently added to the group.

clearLayers() this

Removes all the layers from the group.

invoke(<String> methodName, ) this

Calls methodName on every layer contained in this group, passing any additional parameters. Has no effect if the layers contained do not implement methodName.

eachLayer(<Function> fn, <Object> context?) this

Iterates over the layers of the group, optionally specifying context of the iterator function.

group.eachLayer(function (layer) {
    layer.bindPopup('Hello');
});
getLayer(<Number> id) Layer

Returns the layer with the given internal ID.

getLayers() Layer[]

Returns an array of all the layers added to the group.

setZIndex(<Number> zIndex) this

Calls setZIndex on every layer contained in this group, passing the z-index.

getLayerId(<Layer> layer) Number

Returns the internal ID for a layer

Method Returns Description
bindPopup(<String|HTMLElement|Function|Popup> content, <Popup options> options?) this

Binds a popup to the layer with the passed content and sets up the neccessary event listeners. If a Function is passed it will receive the layer as the first argument and should return a String or HTMLElement.

unbindPopup() this

Removes the popup previously bound with bindPopup.

openPopup(<LatLng> latlng?) this

Opens the bound popup at the specificed latlng or at the default popup anchor if no latlng is passed.

closePopup() this

Closes the popup bound to this layer if it is open.

togglePopup() this

Opens or closes the popup bound to this layer depending on its current state.

isPopupOpen() boolean

Returns true if the popup bound to this layer is currently open.

setPopupContent(<String|HTMLElement|Popup> content) this

Sets the content of the popup bound to this layer.

getPopup() Popup

Returns the popup bound to this layer.

Method Returns Description
bindTooltip(<String|HTMLElement|Function|Tooltip> content, <Tooltip options> options?) this

Binds a tooltip to the layer with the passed content and sets up the neccessary event listeners. If a Function is passed it will receive the layer as the first argument and should return a String or HTMLElement.

unbindTooltip() this

Removes the tooltip previously bound with bindTooltip.

openTooltip(<LatLng> latlng?) this

Opens the bound tooltip at the specificed latlng or at the default tooltip anchor if no latlng is passed.

closeTooltip() this

Closes the tooltip bound to this layer if it is open.

toggleTooltip() this

Opens or closes the tooltip bound to this layer depending on its current state.

isTooltipOpen() boolean

Returns true if the tooltip bound to this layer is currently open.

setTooltipContent(<String|HTMLElement|Tooltip> content) this

Sets the content of the tooltip bound to this layer.

getTooltip() Tooltip

Returns the tooltip bound to this layer.

Method Returns Description
addTo(<Map> map) this

Adds the layer to the given map

remove() this

Removes the layer from the map it is currently active on.

removeFrom(<Map> map) this

Removes the layer from the given map

getPane(<String> name?) HTMLElement

Returns the HTMLElement representing the named pane on the map. If name is omitted, returns the pane for this layer.

Method Returns Description
on(<String> type, <Function> fn, <Object> context?) this

Adds a listener function (fn) to a particular event type of the object. You can optionally specify the context of the listener (object the this keyword will point to). You can also pass several space-separated types (e.g. 'click dblclick').

on(<Object> eventMap) this

Adds a set of type/listener pairs, e.g. {click: onClick, mousemove: onMouseMove}

off(<String> type, <Function> fn?, <Object> context?) this

Removes a previously added listener function. If no function is specified, it will remove all the listeners of that particular event from the object. Note that if you passed a custom context to on, you must pass the same context to off in order to remove the listener.

off(<Object> eventMap) this

Removes a set of type/listener pairs.

off() this

Removes all listeners to all events on the object.

fire(<String> type, <Object> data?, <Boolean> propagate?) this

Fires an event of the specified type. You can optionally provide an data object — the first argument of the listener function will contain its properties. The event might can optionally be propagated to event parents.

listens(<String> type) Boolean

Returns true if a particular event type has any listeners attached to it.

once() this

Behaves as on(…), except the listener will only get fired once and then removed.

addEventParent(<Evented> obj) this

Adds an event parent - an Evented that will receive propagated events

removeEventParent(<Evented> obj) this

Removes an event parent, so it will stop receiving propagated events

addEventListener() this

Alias to on(…)

removeEventListener() this

Alias to off(…)

clearAllEventListeners() this

Alias to off()

addOneTimeEventListener() this

Alias to once(…)

fireEvent() this

Alias to fire(…)

hasEventListeners() Boolean

Alias to listens(…)

Functions

Function Returns Description
addData(data) Layer Adds a GeoJSON object to the layer.
resetStyle(layer) Layer Resets the given vector layer's style to the original GeoJSON style, useful for resetting style after hover events.
setStyle(style) Layer Changes styles of GeoJSON vector layers with the given style function.
geometryToLayer(<Object> featureData, <GeoJSON options> options?) Layer Creates a Layer from a given GeoJSON feature. Can use a custom pointToLayer and/or coordsToLatLng functions if provided as options.
coordsToLatLng(<Array> coords) LatLng Creates a LatLng object from an array of 2 numbers (longitude, latitude) or 3 numbers (longitude, latitude, altitude) used in GeoJSON for points.
coordsToLatLngs(<Array> coords, <Number> levelsDeep?, <Function> coordsToLatLng?) Array Creates a multidimensional array of LatLngs from a GeoJSON coordinates array. levelsDeep specifies the nesting level (0 is for an array of points, 1 for an array of arrays of points, etc., 0 by default). Can use a custom coordsToLatLng function.
latLngToCoords(<LatLng> latlng) Array Reverse of coordsToLatLng
latLngsToCoords(<Array> latlngs, <Number> levelsDeep?, <Boolean> closed?) Array Reverse of coordsToLatLngs closed determines whether the first point should be appended to the end of the array to close the feature, only used when levelsDeep is 0. False by default.
asFeature(<Object> geojson) Object Normalize GeoJSON geometries/features into GeoJSON features.
v1.1.0
Props Wrld.Prop
Themes Wrld.themes
Heatmaps Wrld.Heatmap
Events Event objects
Services (Optional) WrldPoiApi