Legacy
Mapbox.js is no longer in active development. To learn more about our newer mapping tools see Mapbox GL JS.
GeoJson
Represents a GeoJSON layer. Allows you to parse GeoJSON data and display it on the map. Extends FeatureGroup.
L.geoJson(data, {
style: function (feature) {
return {color: feature.properties.color};
},
onEachFeature: function (feature, layer) {
layer.bindPopup(feature.properties.description);
}
}).addTo(map);
Each feature layer created by it gets a feature
property that links to the GeoJSON feature data the layer was created from (so that you can access its properties later).
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 | Description |
---|---|
pointToLayer(
|
Function that will be used for creating layers for GeoJSON points (if not specified, simple markers will be created). |
style(
|
Function that will be used to get style options for vector layers created for GeoJSON features. |
onEachFeature(
|
Function that will be called on each created feature layer. Useful for attaching events and popups to features. |
filter(
|
Function that will be used to decide whether to show a feature or not. |
coordsToLatLng(
|
Function that will be used for converting GeoJSON coordinates to LatLng points (if not specified, coords will be assumed to be WGS84 — standard [longitude, latitude] values in degrees). |
Additionally accepts all Path options for polylines and polygons.
Methods
Method | Returns | Description |
---|---|---|
addData(
|
this |
Adds a GeoJSON object to the layer. |
setStyle(
|
this |
Changes styles of GeoJSON vector layers with the given style function. |
resetStyle(
|
this |
Resets the given vector layer's style to the original GeoJSON style, useful for resetting style after hover events. |
Static methods
Method | Returns | Description |
---|---|---|
geometryToLayer(
|
ILayer |
Creates a layer from a given GeoJSON feature. |
coordsToLatLng(
|
LatLng |
Creates a LatLng object from an array of 2 numbers (latitude, longitude) used in GeoJSON for points. If reverse is set to true , the numbers will be interpreted as (longitude, latitude). |
coordsToLatLngs(
|
Array |
Creates a multidimensional array of LatLng objects 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). If reverse is set to true , the numbers will be interpreted as (longitude, latitude). |