谷歌地图api v3 - “Infowindow未定义”错误

时间:2012-06-02 23:08:27

标签: javascript google-maps-api-3

地图出现并显示该点。我的标题也出现了。但是一旦我点击标记获取信息,就不会出现任何信息。萤火虫信息如下。

信息是通过数据库引入的,有多个项目;多个标记在地图上显示为应有的。

任何帮助都会得到帮助。感谢..

Firebug Point信息:

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错误:     infowindow没有定义     infowindow.open(地图,标记物);

代码:

<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>

2 个答案:

答案 0 :(得分:4)

改为大写W对我来说还不够。只打开了一个位置。我用两点测试了你的代码:

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";

我猜你在任何时候都只想在屏幕上显示一个InfoWindow。然后,应声明一个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);
  });
}

弹出许多InfoWindows的替代方案需要更改单击侦听器,以便保留对每个InfoWindow的引用。使用围绕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));
}

答案 1 :(得分:3)

infowindow!= infoWindow

你刚用资本宣布它,试图在没有

的情况下使用它