1. 53
    Replace the Default React Leaflet Location Markers with a Custom Image
    1m 2s

Replace the Default React Leaflet Location Markers with a Custom Image

Colby Fayock
InstructorColby Fayock
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

Now's the fun part! With our configuration, we can now substitute the default markers with our own custom ones.

For this lesson, I've provided you with a custom image that you can use:

src/assets/images/utensils-marker.png

Click here for exercise instructions

Colby Fayock: [0:00] Now that we've recreated our markers using the pointToLayer property, we can set up a custom image. On our Leaflet marker instance, we're going to create a new options object.

[0:07] Right here, we're going to specify an icon property and we're going to set that equal to a new Leaflet icon. Inside of that icon, we're going to create an options object where we're going to specify an icon URL, which is going to be where we're going to specify our marker image.

[0:19] Our marker exists in assets shared in utensil marker. We're going to import our image in as utensils icon from that location. As our icon URL, we can specify that variable. If we look at the map, we can see that we see our images but they're not quite the right size.

[0:33] We want to set the images to be the same size as our previous markers. We're going to set an icon size and we're going to set it to a value of an array of 26 by 26.

[0:41] If we look at our markers, we can see that they're back at the right location. If we try to click them, we can see that the popup covers up the markers, not like they did before. Since the height is a little bit different, we need to change our popupAnchor. We're going to set up a popupAnchor property, which we're going to set to an array of and -15.

[0:58] If we look back at our map and click our marker, we can see that our popup moved up 15 pixels.

egghead
egghead
~ 19 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today