Leaflet custom icon, react-leaflet, can't build Gatsby project - leaflet

I've got custom icons working in my Leaflet map just fine when I run gatsby develop locally, but when I try to build I get an error related to my custom marker icon:
WebpackError: TypeError: leaflet_src_layer_marker__WEBPACK_IMPORTED_MODULE_4__.Icon is not a constructor
Related imports:
import { Map, Marker, Popup, TileLayer, Tooltip, ZoomControl } from 'react-leaflet'
import { Icon } from 'leaflet/src/layer/marker'
Create custom icon:
// Init custom map icon
const mapIcon = MapIcon();
const markerIcon = new Icon({
iconUrl: mapIcon,
iconSize: [36, 36],
iconAnchor: [18, 18],
shadowSize: [36, 36],
shadowUrl: 'https://unpkg.com/leaflet#1.5.1/dist/images/marker-shadow.png',
shadowAnchor: [10, 18],
popupAnchor: [0, -16],
tooltipAnchor: [13, -4]
});
JSX:
{ markerData.length > 0 ? markerData.map((node, index) => (
<Marker position={[node.coords.lat, node.coords.lng]} key={`marker-${index}`} icon={markerIcon}>
<Popup className="leaflet-popup" onOpen={(el) => openPopup(el)}>
<h5 className="popup-location-title">{node.location_title}</h5>
<h6 className="popup-address">{node.address}</h6>
<div className="popup-description" dangerouslySetInnerHTML={{ __html: node.description }}></div>
{!!node.embed ?
<div className="popup-embed">
<Embed url={node.embed} className="popup-media-embed" />
</div>
: null}
</Popup>
<Tooltip className="leaflet-tooltip">
<span className="title">{node.location_title}</span>
<span className="address">{node.address}</span>
</Tooltip>
</Marker>
)) : null }
I've tried all the solutions listed here: https://github.com/Leaflet/Leaflet.markercluster/issues/874. I've also looked at several similar questions. Nothing seems to help. For instance, I also tried importing leaflet like so import * as L from "leaflet" and like so import L from "leaflet". And then creating the new icon like so const markerIcon = L.Icon({, like so const markerIcon = L.icon({, like so const markerIcon = new L.Icon({, and like so const markerIcon = new L.icon({. I can console.log(L), but none of these works.
I would love some assistance.

Got it to build. I had to wrap the icon construction in a check for the window obj:
// Init custom map icon
if (typeof window !== 'undefined') {
const mapIcon = MapIcon();
markerIcon = new Icon({
iconUrl: mapIcon,
iconSize: [36, 36],
iconAnchor: [18, 18],
shadowSize: [36, 36],
shadowUrl: 'https://unpkg.com/leaflet#1.5.1/dist/images/marker-shadow.png',
shadowAnchor: [10, 18],
popupAnchor: [0, -16],
tooltipAnchor: [13, -4]
});
}
And again when passing the icon to the marker:
<Marker position={[node.coords.lat, node.coords.lng]} key={`marker-${index}`} icon={(!!markerIcon) ? markerIcon : null}>

Related

React-leaflet trying to add EditControl on my code, but it doesnt work

i want to draw shapes on my map using react-leaflet, but im having issues with editControl, when i use editControl on my code i get the following errors:
Here is the code im using for this page ( still under development) `
console.log(teslaData);
const navigate = useNavigate();
const [age, setAge] = React.useState('');
const [ordem, setOrdem] = React.useState('');
const position = [51.505, -0.09]
const ZOOM_LEVEL = 9;
const mapRef = useRef();
var coordenadas = [
[28.35390453844, -81.56443119049],
[28.35390453844, -81.55619144439],
[28.35983376526, -81.55619144439],
[28.35983376526, -81.56443119049],
[28.35390453844, -81.56443119049],
];
const myIcon = L.icon({
iconUrl: require('../../images/download__1_-removebg-preview.png'),
iconSize: [64, 64],
iconAnchor: [32, 64]
});
const _onDeleted = (e) => {
console.log(e);
}
const _onCreate = (e) => {
console.log(e);
}
const _onEdited = (e) => {
console.log(e);
}
return (
<>
<BarraLateral >
<div className='adjContainerMap'>
<MapContainer center={[51.505, -0.09]} zoom={2} scrollWheelZoom={true}>
<FeatureGroup>
<EditControl
position="topright"
/>
</FeatureGroup>
<TileLayer
attribution='© OpenStreetMap contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<Polygon color="blue" positions={coordenadas} />
{teslaData.map(tesla => (
<Marker
key={tesla.id}
position={[tesla.gps.latitude, tesla.gps.longitude]}
icon={myIcon}
>
</Marker>
))}
</MapContainer>
</div>
</BarraLateral>
</>
);
}
`
i already tried to downgrade leaflet version but it still doesnt work, i installed #react-leaflet/core as it recommends in another stackoverflow question ( still doesnt work) and if i remove editControl the code works fine, is there an alternative to it? does leaflet stop to support editControl? thanks in advance!

How do I add leaflet.markercluster plugin to my code?

I’m wondering if anyone can help me.
I’m pretty new to coding and i’m trying to implement leaflets marker cluster into my site, however nothing is showing up when I add the usage from GitHub.
var BPalace = L.icon({
iconUrl: 'img/icons8-marker-50.png',
iconSize: [30, 30],
iconAnchor: [15, 30],
popupAnchor: [1, -30],
});
var marker1 = L.marker([51.5015385807725, -0.14176521957406812], {
icon: BPalace
}).bindPopup('Buckingham Palace');
var BatterseaPark = L.icon({
iconUrl: 'img/icons8-marker-50.png',
iconSize: [30, 30],
iconAnchor: [15, 30],
popupAnchor: [1, -30],
});
var marker2 = L.marker([51.478988287721876, -0.15648732182296068], {
icon: BatterseaPark
}).bindPopup('Battersea Park');
var wembley = L.icon({
iconUrl: 'img/icons8-marker-50.png',
iconSize: [30, 30],
iconAnchor: [15, 30],
popupAnchor: [1, -30],
});
var marker3 = L.marker([51.55619811605852, -0.2794567696109353], {
icon: wembley
}).bindPopup('Wembley Stadium');
var bAirport = L.icon({
iconUrl: 'img/icons8-marker-50.png',
iconSize: [30, 30],
iconAnchor: [15, 30],
popupAnchor: [1, -30],
});
var marker4 = L.marker([52.452333381857784, -1.7435316209846132], {
icon: bAirport
}).bindPopup('Birmingham Airport');
L.featureGroup([marker1, marker2, marker3, marker4])
.addTo(mymap);
var markers = L.markerClusterGroup();
markers.addLayer(L.marker(marker1(map)));
map.addLayer(markers).addTo(mymap);
Is there something I’m doing wrong? I have added the relevant files to my html page, but when i refresh nothing seems to happen.
Any help will be much appreciated.
Thank you
Your attempt was correct but the code not, change it to:
var markers = L.markerClusterGroup();
marker1.addTo(markers);
marker2.addTo(markers);
marker3.addTo(markers);
marker4.addTo(markers);
markers.addTo(mymap);
And remove:
L.featureGroup([marker1, marker2, marker3, marker4])
.addTo(mymap);

fuzzy Popup in leaflet Maps

I am creating a map with leaflet, but the text of the popup appear fuzzied. ( Windows 7 Enterprise / Leaflet 0.7.7 / Google Chrome Version 51.0.2704.79 m)
<script>
var mymap = L.map('mapid', {
center: [42.9050205, 1.8760075],
zoom: 9
});
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFsfdNDBjZWd2M2x6bDk3c2ZtOTkifQ._QA7i5Mpkd_m30IGElHziw', {
maxZoom: 18,
attribution:
'museodelprado © www.museodelprado.es',
id: 'mapbox.streets'
}).addTo(mymap);
var MuseosIcon = L.Icon.extend({
options: {
shadowUrl: L.Icon.Default.imagePath + '/marker-shadow.png',
iconSize: [38, 95],
shadowSize: [50, 64],
iconAnchor: [22, 94],
shadowAnchor: [4, 62],
popupAnchor: [-3, -76]
}
});
var greenIcon = new MuseosIcon({iconUrl: L.Icon.Default.imagePath + '/marker-icon-2x-green.png'}),
redIcon = new MuseosIcon({iconUrl: L.Icon.Default.imagePath + '/marker-icon-2x-red.png'}),
blueIcon = new MuseosIcon({iconUrl: L.Icon.Default.imagePath + '/marker-icon-2x-blue.png'}),
yellowIcon = new MuseosIcon({iconUrl: L.Icon.Default.imagePath + '/marker-icon-2x-yellow.png'});
L.marker([42.13675,1.93143],{icon: blueIcon,title: 'MAQ. 1'}).addTo(mymap)
.bindPopup( "The Museo del Prado is the main Spanish national art museum, located in central Madrid. <br> It features one of the world's finest collections of European art, <br> dating from the 12th century to the early 19th century, <br> based on the former Spanish Royal Collection").openPopup();
var popup = L.popup();
</script>
my browser zoom was not at 100% (press Ctrl+zero)

custom markers not displaying on mapbox map

I am working with a mapbox map on a web page, and have everything working fine with the map and dropping pins, but cant get custom markers working.
My code that is working for dropping a pin is:
L.mapbox.featureLayer({
type: 'Feature',
geometry: { type:'Point', coordinates:[$longitude, $latitude] },
properties: { title:'My Location', description:'My Description', 'marker-size':'large', 'marker-color':'#cc0000', 'marker-symbol':'building' }
}).addTo(map);
This works and populates the map with my locations.
I have used this code to try and add a custom marker, but this doesnt work:
L.mapbox.featureLayer({
type: 'Feature',
geometry: { type:'Point', coordinates:[$longitude, $latitude] },
properties: { title:'My Location', description:'My Description', icon: { iconUrl: 'https://www.mapbox.com/mapbox.js/assets/images/astronaut1.png', iconSize: [50, 50], iconAnchor: [25, 25], popupAnchor: [0, -25], className: 'dot' } }
}).addTo(map);
I also have multiple pins and other layers on the map that can be turned on or off, and everything is working well, I just cant get this custom marker to work at all!
Does anyone have custom markers working with code like this on mapbox?
I don't think you can specify custom marker options like icon like that in the main GeoJSON properties hash.
I believe you would need to do something like the below instead.
var customIcon = new L.Icon({iconUrl: 'https://www.mapbox.com/mapbox.js/assets/images/astronaut1.png', iconSize: [50, 50], iconAnchor: [25, 25], popupAnchor: [0, -25], className: 'dot' })
L.mapbox.featureLayer(
{
type: 'Feature',
geometry: { type:'Point', coordinates:[$longitude, $latitude] },
properties: { title:'My Location', description:'My Description'}
},
{
pointToLayer: function(feature,latLng) { return L.marker(latLng, {icon: customIcon}}
}).addTo(map);
See https://www.mapbox.com/mapbox.js/api/v2.2.2/l-mapbox-featurelayer/

Leaflet MarkerCluster Spiderfy dynamic line color

I'm using the LeafLet MarkerCluster plugin ( https://github.com/Leaflet/Leaflet.markercluster )
Does anyone know if it is possible to color the MarkerCluster Spiderfy line color dynamically?
For example, I can set other things dynamically like this:
markersFeatureLayer.eachLayer(function(layer) {
layer.feature.properties.line = 'green';
layer.setIcon(L.MyMarkers.icon({
icon: layer.feature.properties.myData.icon,
markerColor: layer.feature.properties['icon-color'],
iconColor: '#FFFFFF'
}));
mapClusterLayer.addLayer(layer);
});
Or this
mapClusterLayer = new L.MarkerClusterGroup({
maxClusterRadius: 2,
spiderfyDistanceMultiplier: 3,
removeOutsideVisibleBounds: false,
iconCreateFunction: function(cluster) {
return new L.DivIcon({
iconSize: [30, 30],
html: '<div style="text-align:center;color:#fff;background:' +
'green' + '">' + cluster.getChildCount() + '</div>',
});
}
});
I want each spider line to be equal to markerColor. So each will have a unique color.
Any ideas?