破碎的代码 - 融合表格地图

时间:2012-05-10 04:35:17

标签: javascript google-fusion-tables

我不知道为什么但是地图画布没有出现在屏幕上...... 我使用Fusion TablesLayer Wizard 2.0添加过滤器(基于文本的搜索)。也许问题是IF ... ELSE声明。或者一些小的语法错误。请帮忙!

<!DOCTYPE html>
<html>
<head>
<style>
   #map-canvas { width:500px; height:400px; }
</style>
<script type="text/javascript" src="http://maps.google.com/maps/apijs?sensor=false">     </script>
<script type="text/javascript">
   var map;
   var layer10;

   function initialize() {
   map = new google.maps.Map(document.getElementById('map-canvas'), {
   center: new google.maps.LatLng(-30.08870187538118, -51.16813659667969),
   zoom: 11,
   mapTypeId: google.maps.MapTypeId.ROADMAP
});
   layer10 = new google.maps.FusionTablesLayer({
     query: {
     select: "'ENDERECO_GOOGLE'",
     from: 3767057
     },
   map: map
   });
   }


//the function to activate filters   
   function changeMap10() {
   var searchString = document.getElementById('search-string-10').value.replace(/'/g, "\\'");
   var filter = document.getElementById('filter').value.replace(/'/g, "\\'");
   if (filter == "TURNO"){
     layer10.setOptions({
       query: {
       select: "'ENDERECO_GOOGLE'",
       from: 3767057,
       }  
     });
   } 
     layer10.setOptions({
       query: {
       select: "'ENDERECO_GOOGLE'",
       from: 3767057,
       where: "'TURNO'" = '" + searchString + "'"
       }  
     });
   else if (filter == "DIA_SEMANA"){
     layer10.setOptions({
       query: {
       select: "'ENDERECO_GOOGLE'",
       from: 3767057,
       }
     });
   }
     layer10.setOptions({
       query: {
       from: 3767057,
       where: "'DIA_SEMANA'" = '" + searchString + "'"
       }        
     }); 
   }
   google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
   <div id="map-canvas"></div>
   <div style="margin-top: 10px;">
   <label>Choose data!</label><input type="text" id="search-string-10">
   <input type="button" onclick="changeMap10()" value="Search">
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

你的if / else语句有点混淆:你在语句之间有代码。如果我理解您的代码是正确的,您希望根据search-string-10输入更改查询的where子句。我简化了你的代码:

function changeMap10() {
    var searchString = document.getElementById('search-string-10').value.replace(/'/g, "\\'");
    var filter = document.getElementById('filter').value.replace(/'/g, "\\'");
    var whereClause = '';
    if (filter == "TURNO") {
        whereClause = "'TURNO' = '" + searchString + "'";
    }
    else if (filter == "DIA_SEMANA") {
        whereClause = "'DIA_SEMANA' = '" + searchString + "'";
    }
    layer10.setOptions({
        query: {
            select: "'ENDERECO_GOOGLE'",
            from: 3767057,
            where: whereClause
        }
    });
}​

我添加your example on jsFiddle并进行了一些修改,例如添加过滤器选择框或使用contains ignoring case代替=