3

I have been using leaflet to allow geo-results to render onto a map for users to see where their results are located. This has worked fine up until now because I have been using the base functionality of the map.

import React from "react";
import { Map, Marker, Popup, TileLayer } from "react-leaflet";


   const MapComp = (props) => {

        return (
            <div id='map'>
                <Map center={[0, 0]} zoom={0}>
                    <TileLayer
                        url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
                        attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
                    />
                    {
                        props.locations.map(marker => (
                        <Marker
                            key={marker.props.id}
                            position={ [
                                marker.lat,
                                marker.long
                            ]}
                            
                        >
                            <Popup>
                                <b>Media ID:</b> {marker.props.values.mediaId}
                                <br/>
                                <b>Created Date:</b> {marker.props.values.createdDate}
                                <br/>
                                <b>Media URL:</b> <button onClick={()=> window.open(marker.props.values.mediaURL)}>Link</button>
                            </Popup>
                        </Marker>
                    ))}
                </Map>
            </div>
        );

};

export default MapComp;

The error "L is not defined" appears when I attempt to implement leaflet-draw. In order to do this, I need to create a feature group of drawnItems. To do this, I need to use L.FeatureGroup(). For simplicity, I can demonstrate how just one line will cause the app to not compile.

const drawnItems = new L.FeatureGroup();

Compilation Error

The index.html is as follows:

    <!DOCTYPE html>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
      integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A=="
      crossorigin=""/>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
        integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
        crossorigin=""></script>

<title>React App</title>

Along with the leaflet-map css:

.leaflet-container {
width: 90%;
height: 75vh;
border-radius: 20px;
margin-left: 0px;
margin-top: 10px;

}

I have attempted to source the leaflet library locally by putting it into the project directory but this only caused more issues with leaflets styling, it clearly was an issue on my end but after some investigation to other stack overflow questions, I saw other people having the same troubles regardless of how they accessed the leaflet library. I have followed all steps on the leaflet quick start guide, to no avail.

Any help would be appreciated!

Thanks.

2
  • You need to import L using import L from 'leaflet'
    – kboul
    Commented Sep 17, 2020 at 20:32
  • I was trying that and using 'react-leaflet' which was my issue. Thanks!
    – BurgMan
    Commented Sep 17, 2020 at 21:40

2 Answers 2

3
import * as L from "leaflet";

this should solve the problem

0

import * as L from "leaflet";

https://react-leaflet.js.org/docs/example-events/

for detailed usage of map

1
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Commented Jan 31, 2022 at 7:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.