Error in viewing multiple Google Maps markers PHP, JS, HTML

Asked

Viewed 129 times

-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 the problem if the markers aren’t all on Google Maps?

  • How you are iterating over the bookmark list and printing the bookmark?

  • I’m printing the marker across the line var myCenter = new google.maps.LatLng('.$users_dumps_map[$dump['container_id']].');

  • You are printing only one marker.

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>';
?>
  • markers is a variable of the type array where I’ll keep the markers. The push is a method to add something inside the array, in case I am adding the markings of the google maps

  • I made a change in the foreach, take a look at the change and try again.

  • the mark is already ok? only missing values, correct?

1

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

Each marker must be a new object google.maps.Map. Since you are printing via PHP, you can iterate the data via 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 basic one, you must iterate over each location and create a new marker by linking to the map. I omitted the rest of the code, because it will remain virtually the same.

  • @Tiagogonçalves Indifferent, this is just an example of what is wrong with your script, correct your reality.

  • I updated the example for the form you are registering, but just adapt to your code.

  • What error? Update your reply. What’s more, the code you posted in the comment doesn’t make much sense.

Browser other questions tagged

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