Skip to content
the poor coder

lefletjs react

Poor Man
Published
Reading time
2 min read
On this page

Using LeafletJS with React#

If you are looking to integrate mapping functionality into your React application, LeafletJS is a great library to consider. It provides an easy-to-use API for creating interactive maps and can be easily integrated into your React components.

Installation#

To get started, you will need to install LeafletJS and React-Leaflet:

Bash
npm install leaflet react-leaflet

Usage#

To use LeafletJS with React, you will need to create a React component that renders a map using LeafletJS:

JavaScript

import React from 'react';
import { MapContainer, TileLayer } from 'react-leaflet';

function Map() {
  return (
    <MapContainer center={[51.505, -0.09]} zoom={13} scrollWheelZoom={false}>
      <TileLayer
        url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        attribution="&copy; OpenStreetMap contributors"
      />
    </MapContainer>
  );
}

export default Map;

In this example, we are creating a basic map that centers on London and uses OpenStreetMap tiles. The MapContainer component is the main container for the map and the TileLayer component adds the map tiles.

You can also add markers, popups, and other interactive features to your map using LeafletJS:

JavaScript

import React from 'react';
import { MapContainer, TileLayer, Marker, Popup } from 'react-leaflet';

function Map() {
  const position = [51.505, -0.09];

  return (
    <MapContainer center={position} zoom={13} scrollWheelZoom={false}>
      <TileLayer
        url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        attribution="&copy; OpenStreetMap contributors"
      />
      <Marker position={position}>
        <Popup>
          A pretty CSS3 popup. <br /> Easily customizable.
        </Popup>
      </Marker>
    </MapContainer>
  );
}

export default Map;

In this example, we are adding a marker to the map and a popup to the marker.

Alternatives#

While LeafletJS is a popular choice for integrating mapping functionality into React, there are other libraries and frameworks to consider as well:

It's always a good idea to consider your specific use case and requirements before choosing a library or framework.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC