I tried everything I found on the web, Stackoverflow and Github, and I still can't make it.
I want to make a custom marker with a custom icon, but with my code below I always got an error : 'TypeError: options.icon.createIcon is not a function'
Here is my code (no error on the paths to folders, everything is in src/js or src/img)
Icon.js
import L from 'leaflet'; const iconPerson = L.Icon.extend({ options: { iconUrl: require('../img/marker-pin-person.svg'), iconRetinaUrl: require('../img/marker-pin-person.svg'), iconAnchor: null, popupAnchor: null, shadowUrl: null, shadowSize: null, shadowAnchor: null, iconSize: new L.Point(60, 75), className: 'leaflet-div-icon' } }); export { iconPerson };
MarkerPinPerson
import React from 'react'; import { Marker } from 'react-leaflet'; import { iconPerson } from './Icons'; export default class MarkerPinPerson extends React.Component { render() { return ( <Marker position={this.props.markerPosition} icon={ iconPerson } > </Marker> ); } }
Really looking for your help !
If you have the leaflet and react-leaflet npm modules installed, this should work for you. Click on the marker and you will see the popup with a "Change Marker Color" button.
Leaflet is a lightweight, open source mapping library that utilizes OpenStreetMap, a free editable geographic database. In this article, we'll see how to use React Leaflet to render Leaflet maps inside a React app. We'll show markers with custom icons and display a popup on the map when clicked.
The Clusterer can handle 10,000 or even 50,000 markers (in chrome).
I finally found the correct code for the Icon.js file :
import L from 'leaflet'; const iconPerson = new L.Icon({ iconUrl: require('../img/marker-pin-person.svg'), iconRetinaUrl: require('../img/marker-pin-person.svg'), iconAnchor: null, popupAnchor: null, shadowUrl: null, shadowSize: null, shadowAnchor: null, iconSize: new L.Point(60, 75), className: 'leaflet-div-icon' }); export { iconPerson };
I was brought here while trying to figure out how to render a custom icon server side (using react-leaflet-universal). I thought I'd post this in case anyone in the future finds themselves here for the same reason. Just as in the case of react-leaflet-markercluster, I was able to get this working by requiring leaflet inside the return function like:
<Map center={this.props.center} zoom={zoom} className={leafletMapContainerClassName} scrollWheelZoom={false} maxZoom={18} preferCanvas={false} > {() => { const MarkerClusterGroup = require('react-leaflet-markercluster').default; const L = require('leaflet'); const myIcon = L.icon({ iconUrl: require('../assets/marker.svg'), iconSize: [64,64], iconAnchor: [32, 64], popupAnchor: null, shadowUrl: null, shadowSize: null, shadowAnchor: null }); return ( <React.Fragment> <TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" attribution='' setParams={true} /> <MarkerClusterGroup> {coordArray.map(item => { return ( <Marker icon={myIcon} key={item.toString()} position={[item.lat, item.lng]}> {item.title && <Popup> <span>{item.title}</span> </Popup>} </Marker> ) })} </MarkerClusterGroup> </React.Fragment> ); }} </Map>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With