Google Fusion Tables&地图 - 检测区域中的地址

时间:2012-02-01 20:52:44

标签: javascript google-maps kml google-fusion-tables

因此,我目前将Fusion表设置为包含KML区域的地图。我还有一个地址搜索。我需要能够输入一个地址,搜索并确定该点所在的“区域”。这可能吗?提前谢谢。

1 个答案:

答案 0 :(得分:3)

您可以使用一些JavaScript代码执行此操作。首先,听起来你有搜索框工作。您需要对输入到地址搜索中的地址进行地理编码。然后,您可以使用结果的纬度/经度坐标执行交叉查询,以查找融合表中所有特征,这些特征属于输入地址的非常小的半径(例如,0.0001米)。示例代码如下:

<html>
  <head>
    <script type="text/javascript"
        src="http://maps.google.com/maps/api/js?v=3.2&sensor=false&region=US">
    </script>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
      var map, layer;
      var geocoder = new google.maps.Geocoder();
      var tableid = 297050;

      google.load('visualization', '1');

      function initialize() {

        var options = {
          center: new google.maps.LatLng(37.5,-122.23),
          zoom: 10,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        map = new google.maps.Map(document.getElementById('map_canvas'), options);

        layer = new google.maps.FusionTablesLayer({
          query: {
            select: "'Delivery Zone'",
            from: tableid
          },
          map: map
        });

        window.onkeypress = enterSubmit;
      }

      function enterSubmit() {
        if(event.keyCode==13) {
          geocode();
        }
      }

      function geocode() {
        geocoder.geocode({address: document.getElementById('address').value }, findStore);
      }

      function findStore(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {        
          var coordinate = results[0].geometry.location;

          marker = new google.maps.Marker({
            map: map,
            layer: layer,
            animation: google.maps.Animation.DROP,
            position: coordinate
          });

          var queryText = encodeURIComponent("SELECT 'Store Name' FROM " + tableid +
              " WHERE ST_INTERSECTS('Delivery Zone', CIRCLE(LATLNG(" +
              coordinate.lat() + "," + coordinate.lng() + "), 0.001))");
          var query = new google.visualization.Query(
              'http://www.google.com/fusiontables/gvizdata?tq=' + queryText);
          query.send(showStoreName);
        }
      }

      function showStoreName(response) {
        if(response.getDataTable().getNumberOfRows()) {
          var name = response.getDataTable().getValue(0, 0);
          alert('Store name: ' + name);
        }
      }
    </script>
  </head>
  <body onload="initialize()">
    <input type="text" value="Palo Alto, CA" id="address">
    <input type="button" onclick="geocode()" value="Go">
    <div id="map_canvas" style="width:940; height:800"></div>
  </body>
</html>

请注意,如果圆与2个多边形相交,则可能得到2个结果,或者由于半径不为0,您可能会得到误报。