平移和缩放以使用Fusion Table Layer显示查询结果

时间:2012-02-05 09:33:22

标签: javascript google-fusion-tables

我是Fusion Table Layer的初学者。我想在这里显示查询结果(平移和缩放):https://developers.google.com/fusiontables/docs/samples/search_and_zoom,但我不能使用AND子句为我的函数执行此操作:

function changeMap(){

    var dzialka = document.getElementById('dzialka').value;
    var symbol = document.getElementById('symbol').value;
    var where = '';

    if (dzialka) {
      dzialka = dzialka.replace(/'/g, '\\\'');
      where = "'NUMER' CONTAINS IGNORING CASE '" +
          dzialka + "'";
    }

    if (symbol) {
      if (dzialka) {
        where += ' AND ';
      }
      where += "SYMBOL_OG = '" + symbol + "'";
    }

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

有人能帮助我吗?我将非常感谢你的帮助。

TREBOR

1 个答案:

答案 0 :(得分:0)

绝对有可能做你想做的事,但事情并非那么容易。在示例中,平移到的位置由Google Geocoding API使用地址计算。但是你并没有拥有你的JavaScript代码中的地址(即locationColumn的内容)。这取决于您在locationColumn中存储的信息类型,我想这是一种可以进行地理编码的地址。

因此,您必须将select语句直接发送到Fusion Tables。 Google Fusion Tables有一个JSONP界面,您可以将其用于此目的。

var gftUrl = 'http://www.google.com/fusiontables/api/query?';
var jsonUrlTail = '&jsonCallback=?';
var query = 'select ' + locationColumn + ' from ' + tableid + ' where ' + where;
var params = "sql=" + encodeURI(query + jsonUrlTail);

var myCallback = function(data,status) {
    if(status !== 'success') {
        window.alert('Call to Google Fusion Tables failed: ' + status);
        return;
    }
    var address = data.table.rows[0][0];

    /* start code from google example */
    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);
    }
    });
    /* end code from google example */
}

var jqxhr = $.post(gftUrl, params, myCallback, "jsonp"); /* jsonp parameter is very important */

在此示例中,我使用jQuery作为$.post function