0

我正在构建我的第一个融合表,并将两个融合表带入谷歌地图——一个多边形和一个点。当您单击多边形或点时,我不想显示常规信息窗口,而是想从 F​​usion Table 中提取一些数据,但它无法正常工作:

<script>
  function initialize() {
    var england = new google.maps.LatLng(53.2, -2);
    map = new google.maps.Map(document.getElementById('map_canvas'), {
      center: england,
      zoom: 8,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    layer1 = new google.maps.FusionTablesLayer({
      query: {select: 'geometry', from: '1HgGUc72F2Wlp6dvy4GWvLyWrl1kC6aJhnM7jPyc'},
      styles: [
        { where: "Score = 0", polygonOptions: { fillColor: "#cdcdcd", fillOpacity: 0.3 } },
        { where: "Score = 1", polygonOptions: { fillColor: "#FF0000", fillOpacity: 0.5 } },
        { where: "Score = 2", polygonOptions: { fillColor: "#FF7E00", fillOpacity: 0.5 } },
        { where: "Score = 3", polygonOptions: { fillColor: "#008000", fillOpacity: 0.5 } }],
    options: {suppressInfoWindows: true},
    map: map,
    });
    google.maps.event.addListener(layer1, 'click', function(e) {e.infoWindowHtml = "Score" + e.row['Score'].value;}
    });

    layer2 = new google.maps.FusionTablesLayer({
    query: {select: 'postcode', from: '1bawAKwQAUGE4mzYSyw4lTbOcgp7MgIGOLROel8Y'},
    options: {suppressInfoWindows: true},
    map: map,
     });
    google.maps.event.addListener(layer2, 'click', function(e) {e.infoWindowHtml = "Postcode" + e.row['Postcode'].value;}
    });

    google.maps.event.addDomListener(window, 'load', initialize);

  }
</script>
</head>
<body onLoad="initialize()">
  <div id="map_canvas"></div>
</body>
</html>

在我添加 AddListener 和 addDomListeners 之前,一切运行良好,infowindows 被抑制。谁能帮我回到正轨,好吗?

4

2 回答 2

2
  1. 您的“新”代码中有语法错误(两个额外的“}”)
  2. 您可以在初始化函数中调用 addDomListener (没关系,因为您仍然在<body标签中拥有它,但您真的不应该在两个地方都有它。

    <script>
     function initialize() {
     var england = new google.maps.LatLng(53.2, -2);
    map = new google.maps.Map(document.getElementById('map_canvas'), {
      center: england,
      zoom: 8,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    
    layer1 = new google.maps.FusionTablesLayer({
      query: {select: 'geometry', from: '1HgGUc72F2Wlp6dvy4GWvLyWrl1kC6aJhnM7jPyc'},
      styles: [
        { where: "Score = 0", polygonOptions: { fillColor: "#cdcdcd", fillOpacity: 0.3 } },
        { where: "Score = 1", polygonOptions: { fillColor: "#FF0000", fillOpacity: 0.5 } },
        { where: "Score = 2", polygonOptions: { fillColor: "#FF7E00", fillOpacity: 0.5 } },
        { where: "Score = 3", polygonOptions: { fillColor: "#008000", fillOpacity: 0.5 } }],
    options: {suppressInfoWindows: true},
    map: map,
    });
    google.maps.event.addListener(layer1, 'click', function(e) {e.infoWindowHtml = "Score" + e.row['Score'].value;});
    layer2 = new google.maps.FusionTablesLayer({
    query: {select: 'postcode', from: '1bawAKwQAUGE4mzYSyw4lTbOcgp7MgIGOLROel8Y'},
    options: {suppressInfoWindows: true},
    map: map,
     });
     google.maps.event.addListener(layer2, 'click', function(e) {e.infoWindowHtml = "Postcode" + e.row['Postcode'].value;});
     }
     google.maps.event.addDomListener(window, 'load', initialize);
     </script>
     </head>
      <body>
       <div id="map_canvas"></div>
    </body>
    </html>
    

修改工作示例 以使每一层的信息窗口工作,使用此功能:

function openIW(FTevent) {
  // infoWindow.setContent(FTevent.infoWindowHtml);
  // infoWindow.setPosition(FTevent.latLng);
  infoWindow.setOptions(
    { 
     content: FTevent.infoWindowHtml,
     position: FTevent.latLng,
     pixelOffset: FTevent.pixelOffset
    });
  infoWindow.open(map);
}
于 2013-01-13T19:38:24.753 回答
0
google.maps.event.addListener(layer, 'click', function(e) {
    console.log("E.Row! : "+ e.row['name'].value);
});
于 2016-12-26T06:06:45.710 回答