2

I want to add tooltip which will show magnitude for each earthquake epicenter and was able to make it but I have a problem that with many epicenters in one area tooltips are overlapped.

My question is can I first hide tooltips (magnitudes) and then on control when clicked to show them?

This is my code that shows epicenters, and at the end you will see that I have added .bindTooltip.

var earthquakes = L.geoJson([], {
        onEachFeature: function (feature, layer) { 
            var props = feature.properties;
            
        },
        pointToLayer: function (feature, latlng) {
            var color,
                mag,
                radius,
                weight;
                mag = feature.properties.mag;
                label = String(mag);
            if (mag === null) {
                color = '#FF0000';
                radius = 2;
                weight = 1;
            } else {
                color = '#FF0000';
                radius = 2 * Math.max(mag, 1);
                weight = 1;
            }
            if (feature.properties.type === 'quarry blast') {
                color = '#FF00FF';
            }
            return L.circleMarker(latlng, {
                color: color,
                radius: radius,
                weight: weight
            }).bindTooltip(label, {
                permanent: true, 
                direction: 'center',
                opacity: 0.65,
                className: 'pointlabel' 
            }).openTooltip();
        }
    }).addTo(map);

At the beginning of code I have below code for controls where I can switch map and also show lines (earth faults), so can I somehow by using overlays control be able to bintooltip when checked and unbindtooltip when unchecked?

var Faults = new L.LayerGroup();
var Tooltip = new L.LayerGroup();
var Base = {
  "Arcgis map": Arcmap,
  "Google terrain": googlemap
};
var overlays = {
  "Faults": Faults,
  "Tooltip": Tooltip
};

L.control.layers(Base, overlays, {collapsed:false}).addTo(map);

In layer control I would like to have checkbox called tooltip so when checked it show tooltip and when unchecked hide it, see image below:

enter image description here

Here is complete code: https://jsfiddle.net/Tkalac/f5mqo6Lj/1/#&togetherjs=9KtCAh7Unw

10
  • In layer control I would like to have checkbox called tooltip so when checked it show tooltip and when unchecked hide it. Please check image i.ibb.co/D18jVZy/control.jpg
    – Tkalac
    Commented Jan 16, 2022 at 11:34
  • But tooltips are dependant on Faults layer. What if Faults layer is not checked and Tooltips is checked?
    – TomazicM
    Commented Jan 16, 2022 at 14:46
  • Tooltips are not dependant of Faults layer. If I check faults it shows faults, when unchecked it hide it. Tooltip check should show tooltip and when unchecked to hide them. Each work for it self which I did test and instead of tooltip I added layer with some polygon and when checked tooltip polygon show, when unchecked polygon disaper so faults and tool tip as check options are not linked to each other.
    – Tkalac
    Commented Jan 16, 2022 at 17:38
  • Please check example what happen when faults is checked, what happen when Tooltip is checked (I created few lines for example) and what happen when both are checked. They are not linked to each other. i.ibb.co/G5RMtS6/example.jpg
    – Tkalac
    Commented Jan 16, 2022 at 17:55
  • I admit I'm completely lost now. In code I see layer earthquakes with circle markers, then there is layer Faults with no contents, and on the picture there are circle markers (probablay layer earthquakes, which I don't see added to the layer control) and some lines, which are probably coming from layer Faults. Please edit your question, add more code if necessary and try to explain what is what and step by step what should happen.
    – TomazicM
    Commented Jan 16, 2022 at 18:45

1 Answer 1

2

Group layer Tooltip can be used not to actually display tooltips, bust just as a switch in layer control to trigger tooltips display on/off.

When layer Tooltip is selected in the control, it fires layer event add. In event processing function markers from earthquakesEMSC are iterated and tooltips made permanent. When layer Tooltip is deselected in the control, it fires layer event remove. In event processing function markers from earthquakesEMSC are iterated and tooltips made not permanent.

Logic of the code below is that initially tooltips are not displayed.

var earthquakesEMSC = L.geoJson([], {
  onEachFeature: function (feature, layer) { 
    var props = feature.properties;

    var datum = props.time,
    split = datum.split('T');
    var vrijeme = split[1],
    split2 = split[1].split('.'); 
    layer.bindPopup('Date: ' + split[0] + '</br>' + 'Time: ' + split2[0] + '</br>' + 'Type: ' + props.magtype + '</br>' + 'Mag: ' + props.mag + '</br>' + 'Depth: ' + props.depth + ' km' + '</br>' + 'Station: ' + props.auth + '</br>' +'source: ' + props.source_catalog);
  },
  pointToLayer: function (feature, latlng) {
    label = String(feature.properties.mag) 
    var color,
      mag,
      radius,
      weight;
      mag = feature.properties.mag;
      label = String(mag);
    if (mag === null) {
      color = '#FF0000';
      radius = 2;
      weight = 1;
    } else {
      color = '#FF0000';
      radius = 2 * Math.max(mag, 1);
      weight = 1;
    }
    if (feature.properties.type === 'quarry blast') {
      color = '#FF00FF';
    }
    return L.circleMarker(latlng, {
      color: color,
      radius: radius,
      weight: weight
    }).bindTooltip(label, {
      direction: 'center',
      opacity: 0.5,
      className: 'pointlabel'
    });
  }
}).addTo(map);

var Tooltip = new L.LayerGroup();

Tooltip.on('add', function() {
  earthquakesEMSC.eachLayer(function(marker) {
    var tooltip = marker.getTooltip();
    marker.unbindTooltip();
    tooltip.options.permanent = true;
    marker.bindTooltip(tooltip);
  });
});
Tooltip.on('remove', function() {
  earthquakesEMSC.eachLayer(function(marker) {
    var tooltip = marker.getTooltip();
    marker.unbindTooltip();
    tooltip.options.permanent = false;
    marker.bindTooltip(tooltip);
  });
});
1
  • Thank you this works fine. There is one thing I had to change as tooltips showed when map is loaded which I didn't want (only show/hide via controls) so I changed permanent from true to false , bindTooltip(label, { permanent: false,.
    – Tkalac
    Commented Jan 17, 2022 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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