1. 40
    Add React Leaflet Popups to all of our Markers using GeoJSON Data
    44s

Add React Leaflet Popups to all of our Markers using GeoJSON Data

Colby Fayock
InstructorColby Fayock
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

With our data, we'll need a way to present it. Luckily, Leaflet's GeoJSON instance provides ways for us to customize our data on the map so that we can visualize it and communicate more effectively.

Click here for exercise instructions

Colby Fayock: [0:00] Now that we have the data for each location, let's add a popup to each marker. We're going to use a custom option for the GeoJSON instance. We're going to set a property called onEachFeature , which we're going to set equal to a new function.

[0:09] In that function, we have two arguments. We have feature which we're going to set by default to an empty object. We also have layer. Our feature is going to be from our location data. We can destructure those properties straight from that feature. Since each location has a name property, we can destructure that out too.

[0:23] To test that, we can console.log out each of those names. We can see each of our name show up. We want to create a popup, so we're going to create a new constant called popup. We're going to set that equal to a new Leaflet popup. Using that popup, we're going to set its content to the name.

[0:35] Finally, we're going to use the layer which is a Leaflet layer. We're on the layer. We're going to run-bind popup, and pass in our popup. When we reload the page, we can click the marker, and we can see each of those names.

egghead
egghead
~ 22 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