Reputation: 1036
In leaflet some of my markers are built like this;
$div1 = "<div class='xx'>OBJ: $objmrkr<br></div><div class='gg'>W3W: $w3w</div>"; //echo "$div1<br>";
$div2 = "<div class='cc'>Comment:<br>$comment</div>"; //echo "$div2<br>";
$div3 = "<div class='bb'><br>Cross Roads:<br>$cr</div><br><div class='gg'>$row[lat],$row[lng]<br>Grid: $gs<br><br>Captured:<br>$row[timestamp]</div>"; //echo "$div3<br>";
$objMarkers .= " var $objmrkr = new L.marker(new L.LatLng($row[lat],$row[lng]),{
rotationAngle: $dup,
rotationOrigin: 'bottom',
opacity: 0.75,
contextmenu: true,
contextmenuWidth: 140,
contextmenuItems: [{ text: 'Click here to add mileage circles',callback: circleKoords}],
icon: L.icon({iconUrl: '$markername', iconSize: [32, 34]}),
title:`marker_$markNO`}).addTo(fg).bindPopup(`$div1<br>$div2<br>$div3`).openPopup();
$('Objects'._icon).addClass('objmrkr');
";
};
When right clicked on the circleKoords function is called, but before its code is run I need to know if I have the correct marker. The correct markers have an 'OBJ:' in the title like the example. This seems like a clumsy way to do this but I don't know another. My question is how do I verify that 'OBJ:' is in the popup of this marker? There are a number of similar questions on SO but none of them seems to work, I'm not sure if its because I'm using contextmenu or something else. I tried; var imanobj = e.xx kind of things but nothing seems to get what I need? Thoughts?
Upvotes: 0
Views: 108
Reputation: 11348
You need to get the content of the popup (of the marker) marker.getPopup().getContent()
and then you can check with content.indexOf("OBJ") > -1
if it contains the string.
marker.on('contextmenu',(e)=>{
var marker = e.target;
if(marker.getPopup()){
var popup = marker.getPopup();
if(popup.getContent().indexOf("OBJ") > -1){
alert('Clicked Marker has OBJ')
}
}
})
https://jsfiddle.net/falkedesign/54hoaerx/
OBJ
flag:If you want to know if the marker is a OBJ-Marker
, you can simply add a property flag. $objmrkr.objFlag = true;
$objMarkers .= " var $objmrkr = new L.marker(new L.LatLng($row[lat],$row[lng]),{
rotationAngle: $dup,
rotationOrigin: 'bottom',
opacity: 0.75,
contextmenu: true,
contextmenuWidth: 140,
contextmenuItems: [{ text: 'Click here to add mileage circles',callback: circleKoords}],
icon: L.icon({iconUrl: '$markername', iconSize: [32, 34]}),
title:`marker_$markNO`}).addTo(fg).bindPopup(`$div1<br>$div2<br>$div3`).openPopup();
$('Objects'._icon).addClass('objmrkr');
$objmrkr.objFlag = true;
";
marker.on('contextmenu',(e)=>{
var marker = e.target;
if(marker.objFlag){
alert('Clicked Marker is a OBJ-Marker')
}
})
(I have not checked if there any php errors)
$objMarkers .= " var $objmrkr = new L.marker(new L.LatLng($row[lat],$row[lng]),{
rotationAngle: $dup,
rotationOrigin: 'bottom',
opacity: 0.75,
contextmenu: true,
contextmenuWidth: 140,
contextmenuItems: [{ text: 'Click here to add mileage circles',callback: circleKoords}],
icon: L.icon({iconUrl: '$markername', iconSize: [32, 34]}),
title:`marker_$markNO`}).addTo(fg).bindPopup(`$div1<br>$div2<br>$div3`).openPopup();
$('Objects'._icon).addClass('objmrkr');
$objmrkr.on('contextmenu',function(e){
var marker = e.target;
if(marker.getPopup()){
var popup = marker.getPopup();
if(popup.getContent().indexOf('OBJ') > -1){
alert('Clicked Marker has OBJ')
}
}
})";
Upvotes: 2