1

I am using React with leaflet but I do not know how to change the marker's color from blue to red. I looked at the documentation but I didn't find anything on this.

Here is my code :

import React from 'react';
import { render } from 'react-dom';
import Map from './Map';

class App extends React.Component {
  state = { markerPosition: { lat: 49.8419, lng: 24.0315 } };
  moveMarker = () => {
    const { lat, lng } = this.state.markerPosition;
    this.setState({
      markerPosition: {
        lat: lat + 0.0001,
        lng: lng + 0.0001, 
      }
    });
  };
  render() {
    const { markerPosition } = this.state;
    return (
      <div>
        <Map markerPosition={markerPosition} />
        <div>Current markerPosition: lat: {markerPosition.lat}, lng: {markerPosition.lng}</div>
        <button
          onClick={this.moveMarker}
        >
          Move marker
        </button>
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

https://codesandbox.io/s/m4k3x1ynl8

Do you know how can I do this?

Thank you very much!

3
  • The marker is an image, you can replace it with whatever you want. dist/images/marker-icon.png , see this Commented Nov 30, 2020 at 12:38
  • How can I do to import L ?
    – Peter
    Commented Nov 30, 2020 at 14:57
  • @Peter You've already done that in your Map component in the linked sandbox.
    – 3limin4t0r
    Commented Nov 30, 2020 at 20:35

2 Answers 2

0

Since marker is an icon you can change the icon that it use.

      const redIcon = new L.Icon({
      iconUrl:
        "https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-2x-red.png",
      shadowUrl:
        "https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/images/marker-shadow.png",
      iconSize: [25, 41],
      iconAnchor: [12, 41],
      popupAnchor: [1, -34],
      shadowSize: [41, 41]
    });
    // add marker
    this.marker = L.marker(this.props.markerPosition, {
      icon: redIcon
    }).addTo(this.map);

Refer to this for more information: Leaflet changing Marker color

4
  • How can I import L ?
    – Peter
    Commented Nov 30, 2020 at 13:27
  • Coud you see my code in this link : codesandbox.io/s/m4k3x1ynl8
    – Peter
    Commented Nov 30, 2020 at 15:36
  • Yeah, In the Map.js file, line number 24. That's where you are adding the marker. if you remove your current line 24 and copy and paste my code above, it will work correctly Commented Nov 30, 2020 at 15:51
  • codesandbox.io/s/… Commented Nov 30, 2020 at 15:57
0

First you need to create new icon instance like below

const icon = new L.Icon({
  iconUrl:
    "https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-2x-green.png",
  shadowUrl:
    "https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/images/marker-shadow.png",
  iconSize: [25, 41],
  iconAnchor: [12, 41],
  popupAnchor: [1, -34],
  shadowSize: [41, 41]
});

and then you should add it to options

// add marker
this.marker = L.marker(this.props.markerPosition, { icon }).addTo(this.map);
4
  • How can I import L ?
    – Peter
    Commented Nov 30, 2020 at 13:27
  • Actually, I got this ` 'L' is not defined no-undef`
    – Peter
    Commented Nov 30, 2020 at 15:35
  • you can look Map.js
    – Firuz
    Commented Nov 30, 2020 at 17:12
  • import L from "leaflet"
    – Firuz
    Commented Nov 30, 2020 at 17:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.