Google Fusion Tables API

Fusion Tables Layer Example: Autocomplete Text Search


<!DOCTYPE html>
<!--
  Copyright 2011 Google Inc. All Rights Reserved.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="UTF-8">

    <title>Fusion Tables Layer Example: Autocomplete Text Search</title>

    <link href="/apis/fusiontables/docs/samples/style/default.css"
        rel="stylesheet" type="text/css">
    <link rel="stylesheet"
        href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/themes/base/jquery-ui.css"
        type="text/css" />

    <script type="text/javascript"
        src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript"
        src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
    </script>
    <script type="text/javascript"
        src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js">
    </script>

    <script type="text/javascript">
      google.load('visualization', '1');

      function initialize() {
        var map = new google.maps.Map(document.getElementById('map-canvas'), {
          center: new google.maps.LatLng(37.5, -122.1),
          zoom: 10,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        var tableId = '15UY2pgiz8sRkq37p2TaJd64U7M_2HDVqHT3Quw';
        var locationColumn = 'Address';
        var layer = new google.maps.FusionTablesLayer({
          query: {
            select: locationColumn,
            from: tableId
          },
          map: map
        });

        initAutoComplete(tableId);

        // Update layer when user clicks Go.
        google.maps.event.addDomListener(document.getElementById('go'), 'click',
            function() {
              var store = document.getElementById('store').value;

              if (store) {
                store = store.replace(/'/g, '\\\'');
                var where = "'Store Name' CONTAINS IGNORING CASE '" +
                    store + "'";

                layer.setOptions({
                  query: {
                    select: locationColumn,
                    from: tableId,
                    where: where
                  }
                });
              }
        });
      }

      function initAutoComplete(tableId) {
        // Retrieve the unique store names using GROUP BY workaround.
        var queryText = encodeURIComponent(
            "SELECT 'Store Name', COUNT() " +
            'FROM ' + tableId + " GROUP BY 'Store Name'");
        var query = new google.visualization.Query(
            'http://www.google.com/fusiontables/gvizdata?tq='  + queryText);

        query.send(function(response) {
          var numRows = response.getDataTable().getNumberOfRows();

          // Create the list of results for display of autocomplete.
          var results = [];
          for (var i = 0; i < numRows; i++) {
            results.push(response.getDataTable().getValue(i, 0));
          }

          // Use the results to create the autocomplete options.
          $('#store').autocomplete({
            source: results,
            minLength: 2
          });
        });
      }

      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    <div id="map-canvas"></div>

    <div>
      <input type="text" id="store">
      <input type="button" value="Go!" id="go">
      <small>HINT: Try typing "John"</small>
    </div>
  </body>
</html>

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.