Search code examples
jsongoogle-mapsjqgridgoogle-maps-api-3google-fusion-tables

Update jqGrid table with the results of Fusion Tables query in a Google Maps v3 page


I am looking to understand how to update a jqGrid table from Fusion Tables (FT) -

at the moment I can search or scroll on a Google Map, send an event listener that compiles a FT query of the spatial bounds of the viewport/map, to get a new set of results. I want to use the new FT query string (or could use the Google code to retrieve the data - query.send(getData);) to update the jqGrid table with the new values.

Before I started using jqGrid, I tried/suceeded with the Google Visualisation API, and some of that code is below. Could anyone suggest how to move from table.draw, to loading/reloading a jqGrid table? Thanks a lot in advance.

function tilesLoaded() {
        google.maps.event.clearListeners(map, 'tilesloaded');
        google.maps.event.addListener(map, 'zoom_changed', getSpatialQuery);
        google.maps.event.addListener(map, 'dragend', getSpatialQuery);
        getSpatialQuery();  
    }   

    function getSpatialQuery() {
      sw = map.getBounds().getSouthWest();
      ne = map.getBounds().getNorthEast();
      var spatialQuery = "ST_INTERSECTS(latitude, RECTANGLE(LATLNG(" + sw.lat() + "," + sw.lng() + "), LATLNG(" + ne.lat() + "," + ne.lng() + ")))";

      changeDataTable(spatialQuery);
    }

function changeDataTable(spatialQuery) {
  var whereClause = "";
  if(spatialQuery) {
    whereClause =  " WHERE " + spatialQuery;
  }
  var queryText = encodeURIComponent("SELECT 'latitude', 'longitude', 'name' FROM xxxxxxxx" + whereClause + " LIMIT 50");
  var query = new google.visualization.Query('http://www.google.com/fusiontables/gvizdata?tq='  + queryText);
  query.send(getData);
}

function getData(response) {
  var table = new google.visualization.Table(document.getElementById('visualization'));
  table.draw(response.getDataTable(), {showRowNumber: true});
}

Oh, and I used Oleg's code jqGrid returns blank cells as a basis for just seeing if I could get a simple multi-select table to pull data from my FT - that worked fine with the simple mod of

url: 'http://www.google.com/fusiontables/api/query?sql=' +


Solution

  • In case this helps someone, I've taken some of the code I came up with and pasted it below:

    // You can get the map bounds via then pass it via a function (below is hacked from several functions
    sw = map.getBounds().getSouthWest();
    ne = map.getBounds().getNorthEast();
    var whereClause = "ST_INTERSECTS(latitude, RECTANGLE(LATLNG(" + sw.lat() + "," + sw.lng() + "), LATLNG(" + ne.lat() + "," + ne.lng() + ")))";
    
    //construct the URL to get the JSON
    var queryUrlHead = 'http://www.google.com/fusiontables/api/query?sql=';
    var queryUrlTail = '&jsonCallback=?'; // 
    var queryOrderBy = ' ORDER BY \'name\' ASC';
    var queryMain = "SELECT * FROM " + tableid + whereClause + queryOrderBy + " LIMIT 100";
    var queryurl = encodeURI(queryUrlHead + queryMain + queryUrlTail);
    
    //use the constructed URL to update the jqGrid table - this is the part that I didn't know in my above question
    $("#gridTable").setGridParam({url:queryurl});
    $("#gridTable").jqGrid('setGridParam',{datatype:'jsonp'}).trigger('reloadGrid');