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( |
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 :
|
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:
|
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:
|
filter |
Function
| * |
A Function that will be used to decide whether to show a feature or not.
The default is to show all features:
|
coordsToLatLng |
Function
| * |
A Function that will be used for converting GeoJSON coordinates to LatLng s.
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
| ||
layerremove
|
Event | Data | Description |
---|---|---|
popupopen
| ||
popupclose
|
Event | Data | Description |
---|---|---|
tooltipopen
| ||
tooltipclose
|
Methods
Method | Returns | Description |
---|---|---|
setStyle( |
this |
Sets the given path options to each layer of the group that has a |
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 |
addLayer( |
this |
Adds the given layer to the group. |
removeLayer( |
this |
Removes the given layer from the group. |
removeLayer( |
this |
Removes the layer with the given internal ID from the group. |
hasLayer( |
Boolean |
Returns |
clearLayers() |
this |
Removes all the layers from the group. |
invoke( |
this |
Calls |
eachLayer( |
this |
Iterates over the layers of the group, optionally specifying context of the iterator function.
|
getLayer( |
Layer |
Returns the layer with the given internal ID. |
getLayers() |
Layer[] |
Returns an array of all the layers added to the group. |
setZIndex( |
this |
Calls |
getLayerId( |
Number |
Returns the internal ID for a layer |
Method | Returns | Description |
---|---|---|
bindPopup( |
this |
Binds a popup to the layer with the passed |
unbindPopup() |
this |
Removes the popup previously bound with |
openPopup( |
this |
Opens the bound popup at the specificed |
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 |
setPopupContent( |
this |
Sets the content of the popup bound to this layer. |
getPopup() |
Popup |
Returns the popup bound to this layer. |
Method | Returns | Description |
---|---|---|
bindTooltip( |
this |
Binds a tooltip to the layer with the passed |
unbindTooltip() |
this |
Removes the tooltip previously bound with |
openTooltip( |
this |
Opens the bound tooltip at the specificed |
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 |
setTooltipContent( |
this |
Sets the content of the tooltip bound to this layer. |
getTooltip() |
Tooltip |
Returns the tooltip bound to this layer. |
Method | Returns | Description |
---|---|---|
addTo( |
this |
Adds the layer to the given map |
remove() |
this |
Removes the layer from the map it is currently active on. |
removeFrom( |
this |
Removes the layer from the given map |
getPane( |
HTMLElement |
Returns the |
Method | Returns | Description |
---|---|---|
on( |
this |
Adds a listener function ( |
on( |
this |
Adds a set of type/listener pairs, e.g. |
off( |
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 |
off( |
this |
Removes a set of type/listener pairs. |
off() |
this |
Removes all listeners to all events on the object. |
fire( |
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( |
Boolean |
Returns |
once(…) |
this |
Behaves as |
addEventParent( |
this |
Adds an event parent - an |
removeEventParent( |
this |
Removes an event parent, so it will stop receiving propagated events |
addEventListener(…) |
this |
Alias to |
removeEventListener(…) |
this |
Alias to |
clearAllEventListeners(…) |
this |
Alias to |
addOneTimeEventListener(…) |
this |
Alias to |
fireEvent(…) |
this |
Alias to |
hasEventListeners(…) |
Boolean |
Alias to |
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( |
Layer |
Creates a Layer from a given GeoJSON feature. Can use a custom
pointToLayer and/or coordsToLatLng
functions if provided as options. |
coordsToLatLng( |
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 |
Creates a multidimensional array of LatLng s 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( |
Array |
Reverse of coordsToLatLng |
latLngsToCoords( |
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 |
Normalize GeoJSON geometries/features into GeoJSON features. |