Google Fusion Tables API

Fusion Tables Layer Example: Search and Zoom


<!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: Search and Zoom</title>

    <link href="/apis/fusiontables/docs/samples/style/default.css"
        rel="stylesheet" type="text/css">
    <script type="text/javascript"
        src="http://maps.google.com/maps/api/js?sensor=false"></script>

    <script type="text/javascript">

      function initialize() {
        var defaultCenter = new google.maps.LatLng(10, 10);
        var defaultZoom = 2;
        var tableId = '1ZU8D1ASYZMeIise2gboBt-HPt9hX8Y2NIdMfZg';
        var locationColumn = 'lat';
        var geocoder = new google.maps.Geocoder();

        var map = new google.maps.Map(document.getElementById('map-canvas'), {
          center: defaultCenter,
          zoom: defaultZoom,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });

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

        var zoomToAddress = function() {
          var address = document.getElementById('address').value;
          geocoder.geocode({
            address: address
          }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
              map.setCenter(results[0].geometry.location);
              map.setZoom(10);

              // OPTIONAL: run spatial query to find results within bounds.
              var sw = map.getBounds().getSouthWest();
              var ne = map.getBounds().getNorthEast();
              var where = 'ST_INTERSECTS(' + locationColumn +
                  ', RECTANGLE(LATLNG' + sw + ', LATLNG' + ne + '))';
              layer.setOptions({
                query: {
                  select: locationColumn,
                  from: tableId,
                  where: where
                }
              });
            } else {
              window.alert('Address could not be geocoded: ' + status);
            }
          });
        };

        google.maps.event.addDomListener(document.getElementById('search'),
            'click', zoomToAddress);
        google.maps.event.addDomListener(window, 'keypress', function(e) {
          if (e.keyCode == 13) {
            zoomToAddress();
          }
        });
        google.maps.event.addDomListener(document.getElementById('reset'),
            'click', function() {
              map.setCenter(defaultCenter);
              map.setZoom(defaultZoom);
              layer.setOptions({
                query: {
                  select: locationColumn,
                  from: tableId
                }
              });
            });
      }

      google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
    <div>
      <label>Enter an address:</label>
      <input type="text" id="address" value="Mountain View, CA">
      <input type="button" id="search" value="Search!">
      <input type="button" id="reset" value="Reset">
    </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.