Home>

I want to display a window when I click on a marker.
However, when there are multiple markers and you click another marker, it opens at that time
I want the window to disappear

Error message

The javascript console shows this, but what part is this error
I don't know,
I was able to set a marker. Info window does not display properly

ReferenceError: Can't find variable: marker
Applicable source code
var infowindow;
      function initMap () {
        var target = document.getElementById ('target');
        var opts = {
          zoom: 15,
          center: new google.maps.LatLng (33.9565144, 131.2722788)
        };
        map = new google.maps.Map (document.getElementById ("map"), opts);
        infoWindow = new google.maps.InfoWindow ();
      }
      var markerD = [];
      $(function () {
          $.ajax ({
            type: "POST",
            url: "data.php",
            dataType: "json",
            success: function (data) {
              markerD = data;
              setMarker (markerD);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
              alert ('Error:' + errorThrown);
            }
          });
        });
        var map;

        function setMarker (markerData) {
          console.log (markerData);
          console.log (markerData.length);
          // marker generation
          var icon;
          var marker = [];
          for (var i = 0;i

・ https://www.tutorialfor.com/go.php?id=54683
・ Https://developers.google.com/maps/documentation/javascript/infowindows

I tried referring to the above site.

Supplemental information (FW/tool version etc.)

mamp is used.

  • Answer # 1

    Because you can not confirm the behavior, as long as you can see the source

    function setMarker (markerData) {
              console.log (markerData);
              console.log (markerData.length);
              // marker generation
              var icon;
              var marker = [];


    So themarkervariable can only be used withsetMarker

    function markerEvent (i, html) {
            marker [i] .addListener ('click', function () {
              infoWindow.setContent (html);
              infoWindow.open (map, marker [i]);
    });


    var markerD = [];


    Yah

    var map;


    It can be referred if it is defined under the ormarkerEventfunction argument

Related articles