简体   繁体   English

Google Maps api v3-“未定义信息窗口”错误

[英]Google Maps api v3 - “Infowindow not defined” Error

The map comes 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. Firebug信息如下。

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: Firebug点信息:

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); Firebug错误:未定义infowindow 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.com/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>

Changing to a capital W was not enough for me. 更改为大写字母W对我来说还不够。 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. 我猜您在任何给定时间只希望屏幕上有一个InfoWindow。 Then, a single InfoWindow should be declared, with the contents kept inside the marker, and have the contents change as the marker is clicked. 然后,应声明一个InfoWindow,并将其内容保留在标记内,并在单击标记时更改内容。

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. 另一种选择是弹出许多InfoWindows,需要更改单击侦听器,以便保留对每个单独的InfoWindow的引用。 This effect is accomplished with an anonymous function wrapped around the infoWindow.open function (a new function scope is created). 这种效果是通过在infoWindow.open函数周围包裹一个匿名函数来实现的(创建了一个新的函数范围)。

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 infowindow!= infoWindow

You just have declared it with a capital, trying to use it without 您只是用大写字母声明了它,然后尝试不使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM