最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Maps api v3 - "Infowindow not defined" Error - Stack Overflow

programmeradmin4浏览0评论

The map es up and the point appears. I have the title appearing as well. But as soon as I click on the marker to get info, nothing appears. Firebug info is below.

The information is being brought in via a database and there are multiple items; multiple markers are shown on the map as they should.

Any help would be apprecaited. Thanks..

Firebug Point Info:

MarkLat[i] = xx.xxxxxxxxxxxxxx;
MarkLong[i] = -xx.xxxxxxxxxxxxxx;
MarkerTitle[i] = 'Title 1';
Display[i] = '<table><tr><td>Title 1</td></tr><tr><td>Title 1 Address<br />Title 1 City, State Zip</td></tr><tr><td>Title 1 Phone</td></tr><tr><td>Title 1 Email</td></tr><tr><td>Title 1 URL</td></tr></table>';

Firebug Error: infowindow is not defined infowindow.open(map,marker);

Code:

<script type="text/javascript">
  var i = -1;
  var MarkLat=new Array();
  var MarkLong=new Array();
  var MarkerTitle=new Array();
  var Display=new Array();
  var MapCenter = new google.maps.LatLng(xx.xxxxxxxxxxxxxx,-xx.xxxxxxxxxxxxxx)
</script>

<script type="text/javascript">
  var i = i + 1;
  MarkLat[i] = [[Lat]];
  MarkLong[i] = [[Long]];
  MarkerTitle[i] = '[[Title]]';
  Display[i] = '<table><tr><td>[[Title]]</td></tr><tr><td>[[Address]]<br />[[City]], [[State]] [[Zip]]</td></tr><tr><td>[[Phone]]</td></tr><tr><td>[[Email]]</td></tr><tr><td>[[WebURL]]</td></tr></table>';
</script>

<script type="text/javascript" src=""></script>
<script type="text/javascript">
  function initialize() {
    var myOptions = {
      zoom: 12,
      center: MapCenter,
      zoomControl: true,
      zoomControlOptions: {
        position: google.maps.ControlPosition.TOP_RIGHT,
        style: google.maps.ZoomControlStyle.SMALL
      },
      mapTypeControl: true,
      mapTypeControlOptions: {
        style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
      },
      scaleControl: true,
      scaleControlOptions: {
        position: google.maps.ControlPosition.TOP_CENTER
      },
      mapTypeId: google.maps.MapTypeId.ROADMAP,
    };

    var map = new google.maps.Map(document.getElementById('map_canvas'),myOptions);

    for (var i = 0, length = 50; i < length; i++) {
      var latLng = new google.maps.LatLng(MarkLat[i],MarkLong[i]);
      var infoWindow = new google.maps.InfoWindow(Display[i]);

      // Creating a marker and putting it on the map
      var marker = new google.maps.Marker({
        position: latLng,
        map: map,
        title: MarkerTitle[i]
      });

      google.maps.event.addDomListener(marker, 'click', function() {
        infowindow.open(map,marker);
      });
    }
  }

  google.maps.event.addDomListener(window, 'load', initialize);
</script>

The map es up and the point appears. I have the title appearing as well. But as soon as I click on the marker to get info, nothing appears. Firebug info is below.

The information is being brought in via a database and there are multiple items; multiple markers are shown on the map as they should.

Any help would be apprecaited. Thanks..

Firebug Point Info:

MarkLat[i] = xx.xxxxxxxxxxxxxx;
MarkLong[i] = -xx.xxxxxxxxxxxxxx;
MarkerTitle[i] = 'Title 1';
Display[i] = '<table><tr><td>Title 1</td></tr><tr><td>Title 1 Address<br />Title 1 City, State Zip</td></tr><tr><td>Title 1 Phone</td></tr><tr><td>Title 1 Email</td></tr><tr><td>Title 1 URL</td></tr></table>';

Firebug Error: infowindow is not defined infowindow.open(map,marker);

Code:

<script type="text/javascript">
  var i = -1;
  var MarkLat=new Array();
  var MarkLong=new Array();
  var MarkerTitle=new Array();
  var Display=new Array();
  var MapCenter = new google.maps.LatLng(xx.xxxxxxxxxxxxxx,-xx.xxxxxxxxxxxxxx)
</script>

<script type="text/javascript">
  var i = i + 1;
  MarkLat[i] = [[Lat]];
  MarkLong[i] = [[Long]];
  MarkerTitle[i] = '[[Title]]';
  Display[i] = '<table><tr><td>[[Title]]</td></tr><tr><td>[[Address]]<br />[[City]], [[State]] [[Zip]]</td></tr><tr><td>[[Phone]]</td></tr><tr><td>[[Email]]</td></tr><tr><td>[[WebURL]]</td></tr></table>';
</script>

<script type="text/javascript" src="https://maps.googleapis./maps/api/js?sensor=false"></script>
<script type="text/javascript">
  function initialize() {
    var myOptions = {
      zoom: 12,
      center: MapCenter,
      zoomControl: true,
      zoomControlOptions: {
        position: google.maps.ControlPosition.TOP_RIGHT,
        style: google.maps.ZoomControlStyle.SMALL
      },
      mapTypeControl: true,
      mapTypeControlOptions: {
        style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
      },
      scaleControl: true,
      scaleControlOptions: {
        position: google.maps.ControlPosition.TOP_CENTER
      },
      mapTypeId: google.maps.MapTypeId.ROADMAP,
    };

    var map = new google.maps.Map(document.getElementById('map_canvas'),myOptions);

    for (var i = 0, length = 50; i < length; i++) {
      var latLng = new google.maps.LatLng(MarkLat[i],MarkLong[i]);
      var infoWindow = new google.maps.InfoWindow(Display[i]);

      // Creating a marker and putting it on the map
      var marker = new google.maps.Marker({
        position: latLng,
        map: map,
        title: MarkerTitle[i]
      });

      google.maps.event.addDomListener(marker, 'click', function() {
        infowindow.open(map,marker);
      });
    }
  }

  google.maps.event.addDomListener(window, 'load', initialize);
</script>
Share Improve this question edited Jun 3, 2012 at 7:24 dda 6,2132 gold badges27 silver badges35 bronze badges asked Jun 2, 2012 at 23:08 user1424532user1424532 952 silver badges12 bronze badges 1
  • Question: do you plan on having multiple InfoWindows open at the same time (the code above seems to imply that) or having only one open at a time (clicking on different markers closes the existing window)? – Tina CG Hoehr Commented Jun 2, 2012 at 23:35
Add a ment  | 

2 Answers 2

Reset to default 4

Changing to a capital W was not enough for me. Only one location was being opened. I tested your code with two points:

MarkLat = [];
MarkLong = [];
Display = [];
MarkerTitle= [];

MarkLat[0] = 0;
MarkLong[0] = 0;
Display[0] = { content: "hi" };
MarkerTitle[0] = "hello";

MarkLat[1] = 10;
MarkLong[1] = 10;
Display[1] = { content: "hi 2" };
MarkerTitle[1] = "hello 2";

I'm guessing you only want one InfoWindow on the screen at any given time. Then, a single InfoWindow should be declared, with the contents kept inside the marker, and have the contents change as the marker is clicked.

var infoWindow = new google.maps.InfoWindow();
for (var i = 0, length = Display.length; i < length; i++) {
  var latLng = new google.maps.LatLng(MarkLat[i],MarkLong[i]);

  // Creating a marker and putting it on the map
  var marker = new google.maps.Marker({
    position: latLng,
    map: map,
    title: MarkerTitle[i],
    infoWindowContent: Display[i]
  });

  // Notice I used the 'this' keyword inside the listener
  google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(this.infoWindowContent.content);
    infoWindow.open(map,this);
  });
}

The alternative, having many InfoWindows pop up, needs a change to click listener, so that a reference to each individual InfoWindow is preserved. This effect is acplished with an anonymous function wrapped around the infoWindow.open function (a new function scope is created).

for (var i = 0, length = Display.length; i < length; i++) {
  var latLng = new google.maps.LatLng(MarkLat[i],MarkLong[i]);
  var infoWindow = new google.maps.InfoWindow(Display[i]);

  // Creating a marker and putting it on the map
  var marker = new google.maps.Marker({
    position: latLng,
    map: map,
    title: MarkerTitle[i] 
  });

  google.maps.event.addListener(marker, 'click', (function(infoWindow) {
    return function() {
      infoWindow.open(map,this);
    }
  })(infoWindow));
}

infowindow != infoWindow

You just have declared it with a capital, trying to use it without

发布评论

评论列表(0)

  1. 暂无评论