如何使用javascript查询融合表

时间:2012-11-27 01:47:05

标签: google-fusion-tables

我在为我的融合表创建一些函数时遇到了一些困难。我这里有两张桌子:

Points

Parcels

我正在尝试创建许多功能:

1)我希望能够使用下拉菜单选择点图层,以通过其ID号进行选择。

2)我想创建一个函数,当我选择我的观点时,我可以告诉融合表找到距离该点1公里范围内的地块。如果我能改变距离会更好,所以我可以搜索5km。

这是我的代码:

<!DOCTYPE html>
<html>
  <head>
  <style>
    #map-canvas { width:500px; height:400px; }
  </style>
  <script type="text/javascript"
    src="http://maps.google.com/maps/api/js?sensor=false">
  </script>
  <script type="text/javascript">
    var map;
    var layerl0;
    var layerl1;
    function initialize() {
      map = new google.maps.Map(document.getElementById('map-canvas'), {
        center: new google.maps.LatLng(43.94255650099583, -79.53326251471042),
        zoom: 13,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      });
      layerl0 = new google.maps.FusionTablesLayer({
        query: {
          select: "'col2'",
          from: '1-6QDLUk0Xw7weaB0GP1nmgwvXUuaXJCxWEYbO8E'
        },
        map: map,
        styleId: -1,
        templateId: -1
      });
      layerl1 = new google.maps.FusionTablesLayer({
        query: {
          select: "'col2'",
          from: '1BdIN39m70Vo6Sq0Y5lm1s_4Crh1CZpSwRbYMfnM'
        },
        map: map,
        styleId: -1,
        templateId: -1
      });
    }
    function changeMapl0() {
      var searchString = document.getElementById('search-string-l0').value.replace(/'/g, "\\'");
      layerl0.setOptions({
        query: {
          select: "'col2'",
          from: '1-6QDLUk0Xw7weaB0GP1nmgwvXUuaXJCxWEYbO8E',
          where: "'description' CONTAINS IGNORING CASE '" + searchString + "'"
        }
      });
    }
    function changeMapl1() {
      var searchString = document.getElementById('search-string-l1').value.replace(/'/g, "\\'");
      layerl1.setOptions({
        query: {
          select: "'col2'",
          from: '1BdIN39m70Vo6Sq0Y5lm1s_4Crh1CZpSwRbYMfnM',
          where: "'description' = '" + searchString + "'"
        }
      });
    }
    google.maps.event.addDomListener(window, 'load', initialize);
  </script>
  </head>
  <body>
    <div id="map-canvas"></div>
    <div style="margin-top: 10px;">
      <label>Zone: </label><input type="text" id="search-string-l0">
      <input type="button" onclick="changeMapl0()" value="Search">
    </div>
    <div style="margin-top: 10px;">
      <label>SPP_ID: </label>
      <select id="search-string-l1" onchange="changeMapl1(this.value);">
        <option value="">--Select--</option>
        <option value="<html xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:msxsl="urn:schemas-microsoft-com:xslt">

<head>

<META http-equiv="Content-Type" content="text/html">

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

</head>

<body style=

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

查看Fusion Tables API Reference,尤其是ST_DISTANCE。看起来你想要做的是首先在融合表本身中对每个地块进行地理编码(添加地址字段或纬度/长列并运行内置地理编码器),然后你可以SELECT使用{{ 1}}

另外,您可能需要考虑将HTML格式保留在融合表之外 - 这通常是您放在应用程序/模板而不是数据源中的内容(例如,想象一下,如果您想使用相同的数据

的来源

答案 1 :(得分:0)

目前Fusion Tables不支持使用一个表的位置列在另一个表上运行空间查询(至少我不知道这种可能性)。但是,通过将您的点转换为几何图形,然后在您的宗地表上使用ST_INTERSECTS运行空间查询,您仍然可以实现您想要的效果。

步骤:

  • 从用户点击的点获取lat / lng值。可以从Fusion Table或直接from the click event of the map

    GEvent.addListener(map, "click", function(overlay, latLng) {
        var lat = latLng.lat(),
            lng = latLng.lng();
    });
    

这是complete example of getting the lat/lng values from the map

  • 使用此lat / lng值更新宗地图层的查询

     layer.setOptions({
         query: {
             select: location
             from: 
             where: 'ST_INTERSECTS(circle(latlng(lat,lng),radius), location) 
         }
     });
    

请参阅circle example of Fusion Tables