Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
react-leaflet
Advanced tools
The react-leaflet package is a React wrapper for the Leaflet library, which is a popular open-source JavaScript library for interactive maps. It allows developers to easily integrate and manipulate maps within React applications.
Displaying a Map
This code sample demonstrates how to display a basic map using react-leaflet. The MapContainer component is used to create the map, and the TileLayer component is used to add a tile layer from OpenStreetMap.
import { MapContainer, TileLayer } from 'react-leaflet';
function MyMap() {
return (
<MapContainer center={[51.505, -0.09]} zoom={13} style={{ height: '100vh', width: '100%' }}>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution="© <a href='https://www.openstreetmap.org/copyright'>OpenStreetMap</a> contributors"
/>
</MapContainer>
);
}
Adding Markers
This code sample shows how to add a marker to the map. The Marker component is used to place a marker at a specific position, and the Popup component is used to display a popup when the marker is clicked.
import { MapContainer, TileLayer, Marker, Popup } from 'react-leaflet';
function MyMap() {
return (
<MapContainer center={[51.505, -0.09]} zoom={13} style={{ height: '100vh', width: '100%' }}>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution="© <a href='https://www.openstreetmap.org/copyright'>OpenStreetMap</a> contributors"
/>
<Marker position={[51.505, -0.09]}>
<Popup>
A pretty CSS3 popup. <br /> Easily customizable.
</Popup>
</Marker>
</MapContainer>
);
}
Drawing Shapes
This code sample demonstrates how to draw a polygon on the map. The Polygon component is used to create a polygon shape by specifying an array of latitude and longitude coordinates.
import { MapContainer, TileLayer, Polygon } from 'react-leaflet';
function MyMap() {
const polygon = [
[51.505, -0.09],
[51.51, -0.1],
[51.51, -0.12]
];
return (
<MapContainer center={[51.505, -0.09]} zoom={13} style={{ height: '100vh', width: '100%' }}>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution="© <a href='https://www.openstreetmap.org/copyright'>OpenStreetMap</a> contributors"
/>
<Polygon positions={polygon} />
</MapContainer>
);
}
react-map-gl is a React wrapper for Mapbox GL JS, which is a powerful library for interactive maps. It offers more advanced features and customization options compared to react-leaflet, but it requires a Mapbox access token.
google-maps-react is a library for integrating Google Maps into React applications. It provides a simple API for adding maps and markers, but it is dependent on the Google Maps API, which may have usage limits and requires an API key.
react-google-maps is another library for using Google Maps in React applications. It offers a more declarative approach compared to google-maps-react and includes additional features like drawing tools and heatmaps.
React components for Leaflet maps.
In development, use at your own risks.
Tests and documentation still being worked on.
npm install react-leaflet
React and Leaflet are peer dependencies, if you haven't already installed them use:
npm install leaflet react react-leaflet
All components are React wrappers for Leaflet elements and layers, they need a map instance and therefore must be included in a top-level <Map>
component.
Leaflet example
var L = require("leaflet");
var position = [51.505, -0.09];
var map = L.map("map").setView(position, 13);
L.tileLayer("http://{s}.tile.osm.org/{z}/{x}/{y}.png", {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
L.marker(position).addTo(map)
.bindPopup("A pretty CSS3 popup. <br> Easily customizable.");
React-Leaflet
var React = require("react");
var {Map, Marker, Popup, TileLayer} = require("react-leaflet");
var position = [51.505, -0.09];
var map = (
<Map center={position} zoom={13}>
<TileLayer
url="http://{s}.tile.osm.org/{z}/{x}/{y}.png"
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
/>
<Marker position={position}>
<Popup>
<span>A pretty CSS3 popup.<br/>Easily customizable.</span>
</Popup>
</Marker>
</Map>
);
React.render(map, document.getElementById("map-container"));
Note that the <Map>
component creates its own <div>
container for the map, it does not get attached to an existing node.
LatLng: One of [Number, Number]
, {lat: Number, lng: Number}
or {lat: Number, lon: Number}
.
LatLngList: An Array of LatLng.
Bounds: An instance of Leaflet.LatLngBounds or a LatLngList.
Leaflet exposes its own events, different from React. You can listen to them using React-Leaflet by adding a callback to a property prefixed by onLeaflet
. Ex: <Map onLeafletMoveend={this.handleMoveend}>...</Map>
.
Check Leaflet documentation for the events associated to each component.
The properties documented for each component are the ones aimed to be supported (tested and made dynamic when possible) by React-Leaflet.
All other properties are passed as the options
argument to their corresponding Leaflet element and should work fine for static maps, it is however unlikely that they would updated if you change them afterwards.
You can directly access the Leaflet element created by a component using the getLeafletElement()
method on this component. This leaflet element is usually created in componentWillMount()
, except for the <Map>
component where it can only be created after the <div>
container is rendered.
This is the top-level component that must be mounted for children ones to be rendered. Refer to Leaflet documentation for more information about the properties.
Properties
center
(optional LatLng, dynamic): Center of the map. This property is dynamic, if you change it it will be reflected in the map.id
(optional String): The ID of the <div>
container for the map. If you don't provide it, a unique one will be created.maxBounds
(optional Bounds)maxZoom
(optional Number)minZoom
(optional Number)zoom
(optional Number, dynamic)position
(required LatLng, dynamic)The Popup children will be rendered as its content using React.renderToStaticMarkup()
, they must be valid React elements.
position
(optional LatLng, dynamic)url
(required String, dynamic)opacity
(optional Number, dynamic)zIndex
(optional Number, dynamic)url
(required String, dynamic)opacity
(optional Number, dynamic)attribution
(optional String)center
(required LatLng, dynamic)radius
(required Number, dynamic)center
(required LatLng, dynamic)radius
(optional Number, dynamic)positions
(required LatLngList, dynamic)polylines
(required Array of LatLngList, dynamic)positions
(required LatLngList, dynamic)polygons
(required Array of LatLngList, dynamic)bounds
(required Bounds, dynamic)MIT
See LICENSE file.
FAQs
React components for Leaflet maps
The npm package react-leaflet receives a total of 343,763 weekly downloads. As such, react-leaflet popularity was classified as popular.
We found that react-leaflet demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.