最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Leaflet-React : L is not defined - Stack Overflow

programmeradmin1浏览0评论

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/{z}/{x}/{y}.png"
                        attribution='&copy; <a href=";>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 pile.

const drawnItems = new L.FeatureGroup();

The index.html is as follows:

    <!DOCTYPE html>
<link rel="stylesheet" href="/[email protected]/dist/leaflet.css"
      integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A=="
      crossorigin=""/>
<script src="/[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.

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/{z}/{x}/{y}.png"
                        attribution='&copy; <a href="http://osm/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 pile.

const drawnItems = new L.FeatureGroup();

The index.html is as follows:

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

Share Improve this question asked Sep 17, 2020 at 20:02 BurgManBurgMan 672 silver badges9 bronze badges 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
Add a ment  | 

2 Answers 2

Reset to default 3
import * as L from "leaflet";

this should solve the problem

import * as L from "leaflet";

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

for detailed usage of map

发布评论

评论列表(0)

  1. 暂无评论