DEV Community

Cover image for Integrating Next.js with Leaflet.js + Mapbox

Integrating Next.js with Leaflet.js + Mapbox

Tushar saxena on December 05, 2020

Do you want to include interactive maps in your Nextjs application?Then you must have come across Leafletjs. Though Leafletjs is very simple to use...
Collapse
 
isneverdead profile image
Fariz Akbar

just a little add here for others wondering how to capture the position(lat,lng) after the marker is dragged,

i just added a ref to the marker and eventHandlers function, below is the full code

import React, { useState, useEffect, useRef, useMemo, useCallback } from 'react'

const markerRef = useRef(null)
const eventHandlers = useMemo(
        () => ({
            dragend() {
                const marker = markerRef.current
                if (marker != null) {
                    // setPosition(marker.getLatLng())
                    console.log(marker.getLatLng())
                }
            },
        }),
        [],
    )
 useEffect(() => {
        console.log(markerRef.current)
    }, [markerRef.current])

//////////////////////
/// in the html

<MapContainer center={[40.8054, -74.0241]} zoom={14} scrollWheelZoom={false} style={{ height: "400px", width: "100%" }}>
<TileLayer
attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
   <Marker
   eventHandlers={eventHandlers}
   position={[40.8054, -74.0241]}
   draggable={true}
   animate={true}
   ref={markerRef}
   >
       <Popup>
           Hey ! you found me
       </Popup>
   </Marker>
</MapContainer>
Enter fullscreen mode Exit fullscreen mode


so that when the marker is moved, we will get the current position (lat,lng) of that marker

Collapse
 
saman3230 profile image
Saman Soroushnia

Thanks alot , you saved me

Collapse
 
nielsgregers profile image
Niels Gregers Johansen

Fantastic post - really showing some useful insights - found it look for the windows SSR issue

Collapse
 
arevalodev profile image
Arevalo-dev

wow, excelente tu publicacion me ayudo mucho a resolver el problema de ssr de windows!!!

Collapse
 
tesshsu profile image
tess hsu

Hi I install but got ReferenceError: document is not defined

This error happened while generating the page. Any console logs will be displayed in the terminal window.
from file:///C:/Users/TESS/Documents/GitHub/ecoechange/front/node_modules/leaflet/dist/leaflet-src.js (1826:17)

I had tried lot method but still not fingure out, does it happend to you guys? and how to resolve it with react next js ? tks

Collapse
 
tintindas profile image
Upamanyu Das

Hi, I think the issue might be an incorrect import or the Map component being rendered directly instead of the MapWithNoSSR component.

Collapse
 
anshimishra02 profile image
anshimishra02

Good One !!

Collapse
 
vjpr profile image
Vaughan Rouesnel

<MapContainer style={{ height: "100%", width: "100%" }}> was necessary to get it working for me.

Collapse
 
leor37 profile image
Leandro Rocha

Thanks you!

Collapse
 
sabit990928 profile image
Sabit Rakhim

Thanks man, that was helpful

Collapse
 
pavanrajesh profile image
Pavan-Rajesh

Thanks a lot