如何在infowindow中获取商家名称、地址、phone等信息?

How can I get a business name, address, phone and other information in infowindow?

我正在 API V3 中制作 google 网络地图。我希望用户能够单击企业标记并获取显示企业名称、地址、phone 和 URL 的信息窗口。问题是,我只能让 infoWindow 显示公司名称。我将几个教程中的这个(简化版)粘贴在一起并对其进行了修改以适合我的规格:

<!DOCTYPE html>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
  <title>GoogleMapHawthorne test</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>
</head> 
<body>
  <div id="map" style="width: 950px; height: 525px;"></div>

  <script type="text/javascript">

  // locationsW = locations in Wellness layer
    var locationsW = [
      ['Hawthorne Chiropractic and Healing Arts', 45.504718, -122.653155, '1222 SE Division St', '503-231-9879', 'www.hawthornechiropractic.net'],
      ['Hawthorne Wellness', 45.511934, -122.622045, '3942 SE Hawthorne Blvd', '503-235-5484', 'www.hawthornewellness.com'],
    ];

  // make the map
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 14,
      center: new google.maps.LatLng(45.510000, -122.630930),
      mapTypeId: google.maps.MapTypeId.ROADMAP      
    });

    var infowindow = new google.maps.InfoWindow();

// adding wellness markers
    var markerW, w;

    for (w = 0; w < locationsW.length; w++) {  
      markerW = new google.maps.Marker({
        position: new google.maps.LatLng(locationsW[w][1], locationsW[w][2]),
        map: map,
        icon: {
          url: 'https://www.dropbox.com/s/frncryyoxyfecyg/WellnessGreenCross.png?raw=1',
          scaledSize: new google.maps.Size(18, 18)

        }
      });

      google.maps.event.addListener(markerW, 'click', (function(markerW, w) {
        return function() {
          infowindow.setContent(locationsW[w][0]);
          infowindow.open(map, markerW);
        }
      })(markerW, w));
    }

  </script>
</body>
</html>

我试过这样的方法,但显然没有用:

  google.maps.event.addListener(markerW, 'click', (function(markerW, w) {
    return function() {
      infowindow.setContent(locationsW[w][0][3][4][5]);
      infowindow.open(map, markerW);

这似乎是一件很基本的事情,我确信它已经在堆栈的其他地方解决了,但我不知道要搜索什么术语;我可以找到很多关于 infoWindows 的资料,但没有找到关于这个问题的特别资料。

编辑:为什么投反对票?

您必须附加所有数组值,例如:

<!DOCTYPE html>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
  <title>GoogleMapHawthorne test</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>
</head> 
<body>
  <div id="map" style="width: 950px; height: 525px;"></div>

  <script type="text/javascript">

  // locationsW = locations in Wellness layer
    var locationsW = [
      ['Hawthorne Chiropractic and Healing Arts', 45.504718, -122.653155, '1222 SE Division St', '503-231-9879', 'www.hawthornechiropractic.net'],
      ['Hawthorne Wellness', 45.511934, -122.622045, '3942 SE Hawthorne Blvd', '503-235-5484', 'www.hawthornewellness.com'],
    ];

  // make the map
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 14,
      center: new google.maps.LatLng(45.510000, -122.630930),
      mapTypeId: google.maps.MapTypeId.ROADMAP      
    });

    var infowindow = new google.maps.InfoWindow();

// adding wellness markers
    var markerW, w;

    for (w = 0; w < locationsW.length; w++) {  
      markerW = new google.maps.Marker({
        position: new google.maps.LatLng(locationsW[w][1], locationsW[w][2]),
        map: map 
      });

      google.maps.event.addListener(markerW, 'click', (function(markerW, w) {
        return function() {
          infowindow.setContent(locationsW[w][0]+"<br>Address: "+locationsW[w][3]+"<br>Phone: "+locationsW[w][4]+"</br>Email:"+locationsW[w][5]);
          infowindow.open(map, markerW);
        }
      })(markerW, w));
    }

  </script>
</body>
</html>

locationsW[w][0][3][4][5] 表示 5 维数组。所以你需要修复为:

infowindow.setContent(locationsW[w][0] + " - " + locationsW[w][3] + " - " + locationsW[w][4] + " - " + locationsW[w][5]);