Reputation: 67
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='© <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();
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.
Upvotes: 3
Views: 7463
Reputation: 1
import * as L from "leaflet";
https://react-leaflet.js.org/docs/example-events/
for detailed usage of map
Upvotes: 0