Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom marker icon with react-leaflet

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 !

like image 362
arnaudambro Avatar asked Dec 08 '17 23:12

arnaudambro


People also ask

How do you change the marker icon in leaflet react?

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.

Can you use leaflet with react?

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.

How many markers can leaflet handle?

The Clusterer can handle 10,000 or even 50,000 markers (in chrome).


2 Answers

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 }; 
like image 80
arnaudambro Avatar answered Sep 21 '22 08:09

arnaudambro


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> 
like image 42
David Avatar answered Sep 22 '22 08:09

David