Error in displaying multiple Google Maps bookmarks PHP, JS, HTML

-2

I'm trying to make a Google Maps map that shows the location of a garbage dump, information that is contained in the form of coordinates in a database, and the weight of the dump, also contained in the database.

What's wrong with bookmarks not being all on Google Maps?

    
asked by anonymous 12.08.2018 / 22:35

2 answers

2
<?php
echo' <script>

        function myMap() {
            var myCenter = new google.maps.LatLng('.$users_dumps_map[$dump['container_id']].');
            var mapCanvas = document.getElementById("map");
            var mapOptions = {
                center: myCenter,
                zoom: 5
            };
            var map = new google.maps.Map(mapCanvas, mapOptions);
            var markers = [];';

  foreach ($users_dumps_map as $value) {
    $aux = explode(",", $value);
    echo 'markers.push(
            new google.maps.Marker(
              { position: { lat: ' . $aux[0] . ', lng: ' . $aux[1] . ' } , map: map }
            );
          );';
  }

echo '
            var infowindow = new google.maps.InfoWindow({
                content: "'.$dump['quantity'].'"
            });
            infowindow.open(map, marker);
        }

    </script>';
?>
    
16.08.2018 / 01:04
1

You are only printing one marker on the map, so it will only display one.

Each marker must be a new google.maps.Map object. Since you are printing via PHP, you can iterate through PHP:

$latLngList = [
    '40.349859, -8.583798',
    '38.720430, -9.154948',
    '41.159531, -8.659574',
    '40.3497085,-8.5958659'
];

//iteração da lista
foreach($latLngList as $latLng)
{
    echo "var latLng = new google.maps.LatLng($latLng);
    ";

    //criação de cada marcador
    echo "var marker = new google.maps.Marker({position: latLng , map: map});
    ";
}

This is the basics, you should iterate over each location and create a new marker by linking to the map. I have omitted the rest of the code as it will remain pretty much the same.

    
15.08.2018 / 17:11