如何在infowindow获取公司名称,地址,电话和其他信息?

时间:2015-10-29 09:07:07

标签: javascript google-maps google-maps-api-3 infowindow

我在API V3中制作了一个谷歌网络地图。我希望用户能够点击商家标记并获取显示商家名称,地址,电话和网址的infoWindow。问题是,我只能让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的东西,但特别是这个问题没什么。

编辑:为什么选择downvote?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

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