2

地图出现,点出现。我的标题也出现了。但是,只要我单击标记以获取信息,什么都不会出现。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 错误:未定义 infowindow infowindow.open(map,marker);

代码:

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

2 回答 2

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);
  });
}

另一种方法是弹出许多 InfoWindow,需要更改单击侦听器,以便保留对每个单独 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));
}
于 2012-06-02T23:52:50.240 回答
3

信息窗口!= 信息窗口

你刚刚用大写声明它,试图在没有的情况下使用它

于 2012-06-02T23:18:02.340 回答