array does not work when creating Polyline leaflet

Asked

Viewed 50 times

1

marker no mapa

Someone could help me, I’m having difficulty extracting latitude and longitude from a PHP array and adding leaflet (Javascript) to the map. Coordinates are correct with print but only one location is marked.

CODE USED TO RETRIEVE MYSQL DATABASE DATA

<?php

    while ($row = mysqli_fetch_assoc($resultado_alerta)) {

        $imei = $row['imei'];
        $identificacao = $row['identificacao'];
        $lat = $row['latitude'];
        $lon = $row['longitude'];
        $endereco = $row['endereco'];
        $data = $row['data'];
        $hora = $row['hora'];
        $velocidade = $row['velocidade'];
        $tipo = $row['tipo'];
        
        $string = '[' . floatval($row['latitude']) . ', ' .$row['longitude'] . ']';
        
        $lats[] = $lat;
        $lons[] = $lon;
    }
    
    foreach ($lats as $lat) {
        echo $coords = $lat.",".$lon;
        // echo $lon;
        //echo $coord = [$coord];
    }
?> 
<script type="text/javascript">
    var coord = "<?php echo $coords?>";
    var lat = "<?php echo $lat?>";
    var lon = "<?php echo $lon?>"
    console.log(coord);
    
    var map = L.map('map').setView([lat,lon], 13);
    
    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);
    
    L.marker([lat, lon]).addTo(map)
        .bindPopup('A pretty CSS3 popup.<br> Easily customizable.')
        .openPopup();
    
    
        var coords = [[lat +','+lon]];
        for(var i = 0; i < coords.length; i++) {
        console.log(coords[i]);
    }
    var pointA = new L.LatLng(lat, lon);
    var pointB = new L.LatLng(lat, lon);
    var coords = [pointA, pointB];
            
    
    //var coord = "<?php echo $coords ?>";
    alert(coords);
    coords.forEach(function(coordinate) {
        new L.Marker(coordinate).addTo(map);
    });
    var polyline = L.polyline(coords, {color: 'red', className: 'west_side_drive'}).addTo(map);
    
    map.fitBounds(polyline.getBounds());
</script>
  • Good afternoon guys, still managed to solve this problem, someone could help me?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.