React: Leaflet Markers

This entry is part 9 of 9 in the series React: Leaflet
(Last Updated On: )

In this tutorial I will demonstrate how to add a marker to the map. Refer to the documentation for more information.

Before We Begin:

Text:

LayerGroup onAdd Method

  1. //Define the marker. Since this is text based their is no icon but instead a divIcon
  2. this.textMarker = L.marker([20.5, -0.09],{
  3. icon: L.divIcon({
  4. iconSize: [100,16],
  5. iconAnchor: [22, 30],
  6. className: "",
  7. }),
  8. zIndexOffset: 75,
  9. });
  10.  
  11. //Sets the text
  12. this.textMarker.options.icon.options.html = "This is my text";
  13.  
  14. //Adds the text marker to the layer
  15. this.addLayer(this.textMarker);

LayerGroup onRemove Method

  1. //Remove the text marker you just added
  2. this.removeLayer(this.textMarker);

Results: Now you will see as you turn the layer on and off from the context menu the text will show or hide.

Icon:

Import Image:

  1. import myImagefrom "../images/myImage.png";

Create Icon/Marker onAdd Method

  1. //Define the icon you will be using
  2. var myIcon = L.icon({
  3. iconUrl: myImage,
  4. iconSize: [75, 75], // size of the icon
  5. iconAnchor: [22, 94], // point of the icon which will correspond to marker's location
  6. });
  7.  
  8. //Define the icon based marker
  9. this.marker = L.marker([20.5, -40.09],{
  10. icon: myIcon,
  11. opacity: 0.7,
  12. zIndexOffset: 30
  13. });
  14.  
  15. //Adds the icon marker to the layer
  16. this.addLayer(this.marker);

onRemove Method

  1. //Remove the marker you just added
  2. this.removeLayer(this.marker);

Results: Now you will see as you turn the layer on and off from the context menu the icon will show or hide.

Set Latitude/Longitude:

You can set the latitude and longitude in one of two ways.

  1. //Set the latitude and longitude
  2.  
  3. //Method 1:
  4. L.marker([20.5, -0.09])
  5.  
  6. //Method 2:
  7. this.textMarker.setLatLng([20.5, -0.09]);

Event(s):

onClick

  1. marker.on("click", function(e) {
  2. var marker = e.target;
  3. //Do my work here
  4. }.bind(this));

MouseOver

  1. marker.on("mouseover", function(e) {
  2. var marker = e.target;
  3. //Do my work here
  4. }.bind(this));

MouseOut

  1. marker.on("mouseout", function(e) {
  2. var marker = e.target;
  3. //Do my work here
  4. }.bind(this));

Popup:

BindPopup

  1. marker.bindPopup(L.popup(),{
  2. offset: L.point(0,-10) //You can add an offset if you want to
  3. });

OpenPopup

Let’s say you want to open the popup during a mouseover event.

  1. marker.on("mouseover", function(e) {
  2. var marker = e.target;
  3.  
  4. //Set the content to display in the popup
  5. marker.setPopupContent("My Text");
  6. //Now open the popup
  7. marker.openPopup();
  8. }.bind(this));

ClosePopup

Let’s say you want to open the popup during a mouseout event.

  1. marker.on("mouseout", function(e) {
  2. var marker = e.target;
  3. //Close the popup now
  4. marker.closePopup();
  5. }.bind(this));
Series Navigation<< React: Leaflet Icon

One thought on “React: Leaflet Markers”

Comments are closed.