Search code examples
javascripthtmlgoogle-mapsgoogle-maps-api-3

Add info about address in google maps address


i have problem implementing address visualization on markers within m google maps dynamic map.

My code is the following:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Marker Clustering</title>
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>

      function initMap() {

        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 5,
          center: {lat: 44.0, lng: 12.0}
        });

        // Create an array of alphabetical characters used to label the markers.
        var labels = '';

        // Add some markers to the map.
        // Note: The code uses the JavaScript Array.prototype.map() method to
        // create an array of markers based on a given "locations" array.
        // The map() method here has nothing to do with the Google Maps API.
        var markers = locations.map(function(location, i) {
          return new google.maps.Marker({
            position: location,
            label: labels[i % labels.length]
          });
        });
        // Add a marker clusterer to manage the markers.
        var markerCluster = new MarkerClusterer(map, markers,
            {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
      }

      var locations = [
       {lat: 44.5153, lng: 11.3428},
{lat: 44.4926, lng: 11.3657},
{lat: 44.4948, lng: 11.3158}
//input geocode continue
      ]
    </script>
    <script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js">
    </script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBu-916DdpKAjTmJNIgngS6HL_kDIKU0aU&callback=initMap">
    </script>
  </body>
</html>

I want to add on click popup information on each marker, but i cannot manage to do it, i've searched a lot but I'ven't find any working solution.

Someone can help me?


Solution

  • i've edited your code to let it work with info window

    before, initializing the markers add this line of code

     var infoWin = new google.maps.InfoWindow();
    

    this will initialize the info window, now modify your markers initialization to this

       var markers = locations.map(function(location, i) {
                var marker = new google.maps.Marker({
                    position: location,
                    label: labels[i % labels.length]
                });
    
                //// here we are adding the event listner for the markers to open the info window on click
                google.maps.event.addListener(marker, 'click', function(evt) {
                    infoWin.setContent(location.info);
                    infoWin.open(map, marker);
                })
                return marker;
            });  
    

    notice we are reading the info from the location array so change your locations array to include a third parameter "info" like this

      var locations = [{
                lat: 44.5153,
                lng: 11.3428,
                info: 'place 1'
            }, {
                lat: 44.4926,
                lng: 11.3657,
                info: 'place2'
            }, {
                lat: 44.4948,
                lng: 11.3158,
                info: 'place 3'
            }
            //input geocode continue
        ]