D
Note: I will put a suggestion to simplify/clean the code. I won't go in to analyze if the code is correct or if it contains errors.The JavaScript part is 9 similar functions in which only three values change: (1) the icon URL; (2) the marker index; and (3) the text to display on the pop-up. I was initially going to suggest putting those data in an array and passing a variable to know what data to read in the array (which can be particularly convenient if the list is going to change). Something like this:const valores = [
{
iconUrl: "marcadores/marker-icon-violet.png",
markerIndex: 0,
texto: 'Evasión Control'
},
{
iconUrl: "marcadores/marker-icon-green.png",
markerIndex: 0,
texto: 'Emisiones Diesel'
},
...
But then I realized that the values are repeated 3 times: violet, green, yellow, violet, green, yellow, violet, green, yellow 0, 0, 1, 1, 1, 2, 2, 2Evasion Control, Diesel Emissions, Control Taximeters, ...x3Then you can use a simple switch to see what value corresponds in each case. Which is not going to be able to extend so easily, but it will simplify the code considerably:function ActualMarker(indice) {
const iconUrls = [ "marcadores/marker-icon-violet.png", "marcadores/marker-icon-green.png", "marcadores/marker-icon-yellow.png" ];
const textos = ['Evasión Control', 'Emisiones Diesel', 'Control Taximetros'];
const iconUrl = iconUrls[ (indice-1) % 3 ];
const texto = textos[ (indice-1) % 3 ];
const markerIndex = Math.floor( indice / 3 );
rIcon = L.icon({ iconUrl: iconUrl })
for (var i = 0; i < markers.length; ++i) {
L.marker([markers[ markerIndex ].lat, markers[ markerIndex ].lng], {
icon: rIcon
})
.bindPopup( texto ).addTo(cities);
}
}
Now, instead of calling the function ActualMarker1You'd call ActualMarker with parameter 1; for ActualMarker2You'd call ActualMarker(2); etc.For PHP you could do something similar (you have three similar sections in which it only differs the value of the result index, the value of ot and the scripts that are written). But the code is not the same, plus the last case includes something additional.To simplify it, remove it so much if...elseif...else nest and use a switch. Besides, you can put all the echo of each case together because the result is similar to having them separated:// no usas $i en ningún lugar del bucle, ¿seguro que es correcto?
for($i=0; $i<count($f_result); $i++){
switch ($f_result[0]) {
case 95: echo '<script>ActualMarker(1)</script>'; break;
case 98: echo '<script>ActualMarker(2)</script>'; break;
case 102: echo '<script>ActualMarker(3)</script>'; break;
}
switch ($f_result[1]) {
case 95: echo '<script>ActualMarker(4)</script>'; break;
case 98: echo '<script>ActualMarker(5)</script>'; break;
case 102: echo '<script>ActualMarker(6)</script>'; break;
}
switch ($f_result[2]) {
case 95: echo '<script>ActualMarker(7)</script>'; break;
case 98: echo '<script>ActualMarker(8)</script>'; break;
case 102: echo '<script>ActualMarker(9)</script>'; break;
default: echo "esto no esta funcionando bien"; break;
}
}