编程语言
首页 > 编程语言> > javascript – Google地图中的Fusion Table Layer中忽略了“WHERE”子句

javascript – Google地图中的Fusion Table Layer中忽略了“WHERE”子句

作者:互联网

我正在尝试通过API将Google Fusion表格用作Google地图中的图层.只需使用FusionTableLayer()将图层添加到Google地图就行了.我可以看到地图和所有.当我尝试将过滤器(即“where子句”)应用于选择查询或样式部分时,“乐趣”开始.过滤器不起作用!它不会抛出任何错误.地图继续工作.但是结果集不会被过滤掉 – 好像where子句甚至不存在.用于“样式”部分的“where”子句的相同症状.它完全被忽略了.我有三种不同的样式,我想根据过滤条件应用它.所有这些都被忽略了.奇怪的是,样式块中列出的最后一个样式部分将应用于融合表层中的所有要素.我通过切换部分来验证它.我尝试用“col10”之类的引用替换实际的字段名称,但这没有任何区别.

我错过了什么?如何在FusionTableLayer中“启用”WHERE子句的使用,以便它们既可以在Select查询中应用,也可以在Styles部分中应用?

注意:在下面的代码段中,为此帖子插入了(//)注释.我正在开发的实际页面/代码中不存在这些注释.

  layer = new google.maps.FusionTablesLayer({
  map: map,
  heatmap: { enabled: false },
  query: {
     select: "col11",
     from: "1D6d93-0iT2zUCw8IvkbpDPYDx2-jA0ZAWXi07mQD",
     //the following filter in select query does not work! 
     //I replaced col10 with actual field name (shift_id) but still EVERYTHING from the table is returned
     where: "col10 <= 3" 
  },
        styles: [{
          //this where clause has no effect. I've tried replacing shift_id with col10.
          where: "((shift_id != 1) AND (shift_id != 2))",
          polylineOptions: {
            strokeColor: "#FFFFFF",
            strokeWeight: "3"  }
        }, {
          //this where clause has no effect. I've tried replacing shift_id with col10.
          where: "shift_id == 1",
          polylineOptions: {
            strokeColor: "#FF0000",
            strokeWeight: "3"  }
        }, {
          //this where clause has no effect. I've tried replacing shift_id with col10.
          //whichever of these three blocks is listed last is the one that gets applied to the layer.
          where: "shift_id == 2",
          polylineOptions: {
            strokeColor: "#ffbf00",
            strokeWeight: "3"  }
        }] 
});

解决方法:

>“==”不起作用,使用“=”
>不要在主查询中包含where

layer = new google.maps.FusionTablesLayer({
    map: map,
    heatmap: {
        enabled: false
    },
    query: {
        select: "geometry",
        from: "1D6d93-0iT2zUCw8IvkbpDPYDx2-jA0ZAWXi07mQD",
    },
    styles: [{
        where: "((SHIFT_ID != 1) AND (SHIFT_ID != 2))",
        polylineOptions: {
            strokeColor: "#FFFFFF",
            strokeWeight: "3"
        }
    }, {
        where: "SHIFT_ID = 1",
        polylineOptions: {
            strokeColor: "#FF0000",
            strokeWeight: "3"
        }
    }, {
        where: "SHIFT_ID = 2",
        polylineOptions: {
            strokeColor: "#ffbf00",
            strokeWeight: "3"
        }
    }]
});

proof of concept fiddle

代码段:

var geocoder = new google.maps.Geocoder();
var map;

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  layer = new google.maps.FusionTablesLayer({
    map: map,
    heatmap: {
      enabled: false
    },
    query: {
      select: "geometry",
      from: "1D6d93-0iT2zUCw8IvkbpDPYDx2-jA0ZAWXi07mQD",
    },
    styles: [{
      where: "((SHIFT_ID != 1) AND (SHIFT_ID != 2))",
      polylineOptions: {
        strokeColor: "#FFFFFF",
        strokeWeight: "3"
      }
    }, {
      where: "SHIFT_ID = 1",
      polylineOptions: {
        strokeColor: "#FF0000",
        strokeWeight: "3"
      }
    }, {
      where: "SHIFT_ID = 2",
      polylineOptions: {
        strokeColor: "#ffbf00",
        strokeWeight: "3"
      }
    }]
  });
  geocoder.geocode({
    'address': "Winnipeg, Canada"
  }, function(results, status) {
    if (status === google.maps.GeocoderStatus.OK) {
      map.fitBounds(results[0].geometry.viewport);
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>

标签:javascript,google-maps,google-fusion-tables
来源: https://codeday.me/bug/20191003/1846139.html