admin管理员组文章数量:1414628
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='© <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='© <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
2 Answers
Reset to default 3import * 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
本文标签: javascriptLeafletReactL is not definedStack Overflow
版权声明:本文标题:javascript - Leaflet-React : L is not defined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745158831a2645337.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论