Javascript 谷歌地图距离矩阵和融合表

Javascript 谷歌地图距离矩阵和融合表,javascript,google-maps,matrix,distance,google-fusion-tables,Javascript,Google Maps,Matrix,Distance,Google Fusion Tables,我只是设法在地图上的谷歌融合表中可视化标记。现在我想知道从用户指定的点到表中存储的标记的距离。我想用距离矩阵来做这个。中的代码示例对我来说很好,但是,我不知道如何将表中的标记定义为距离矩阵函数中的目标 如上所述,我现在需要我的变量,它调用来自融合表的标记作为目的地,而不是destA和destB 这是我的变量: var schools = new google.maps.FusionTablesLayer({ query: { select: 'geometry', from:

我只是设法在地图上的谷歌融合表中可视化标记。现在我想知道从用户指定的点到表中存储的标记的距离。我想用距离矩阵来做这个。中的代码示例对我来说很好,但是,我不知道如何将表中的标记定义为距离矩阵函数中的目标

如上所述,我现在需要我的变量,它调用来自融合表的标记作为目的地,而不是destA和destB

这是我的变量:

var schools = new google.maps.FusionTablesLayer({
  query: {
    select: 'geometry',
    from: '1mae334i-txYZFixePEiC7lgYYyi4w6qDN87XAyw'    
  },
}); 
下面是谷歌文档中的基本代码

var origin1 = new google.maps.LatLng(55.930385, -3.118425);
var origin2 = "Greenwich, England";
var destinationA = "Stockholm, Sweden";
var destinationB = new google.maps.LatLng(50.087692, 14.421150);

var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
  origins: [origin1, origin2],
  destinations: [destinationA, destinationB],
  travelMode: google.maps.TravelMode.DRIVING,
  avoidHighways: false,
  avoidTolls: false
}, callback);

function callback(response, status) {
}
如果有人能帮我,我会很高兴的。我想应该有一些非常直接的解决方案,但我不明白:/


无论如何,非常感谢你的帮助

您需要查询FusionTable中的位置,并将查询中的位置用于距离矩阵。由于表中的行不到500行,我可能会使用google可视化库,但新的JSONPAPI也应该可以工作

距离矩阵限制为25个目的地。对于包含94行的表,概念验证比这更多的内容会遇到查询限制和配额问题

获取前25个结果的代码:

  // query the table for the destinations
  var queryString ="SELECT 'geometry' FROM "+FT_TableID;
  var queryText = encodeURIComponent(queryString);
  var query = new google.visualization.Query('http://www.google.com/fusiontables/gvizdata?tq='  + queryText);
  //set the callback function
  query.send(createDestinations);
}

function createDestinations(response) {
  if (!response) {
    alert('no response');
    return;
  }
  if (response.isError()) {
    alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
    return;
  } 
  FTresponse = response;
  //for more information on the response object, see the documentation
   //http://code.google.com/apis/visualization/documentation/reference.html#QueryResponse
  numRows = response.getDataTable().getNumberOfRows();
  numCols = response.getDataTable().getNumberOfColumns();
  var geoXml = new geoXML3.parser();
  var bounds = new google.maps.LatLngBounds();
  var request=0;
  destinations[0] = [];
  for (var i=0; ((i<numRows) && (i<25)); i++) {
    var kml = FTresponse.getDataTable().getValue(i,0);
    geoXml.parseKmlString("<Placemark>"+kml+"</Placemark>");
    destinations[request].push(geoXml.docs[i].markers[0].getPosition());
    bounds.extend(geoXml.docs[i].markers[0].getPosition());
  }
  map.fitBounds(bounds);
  calculateDistances(0);
}

function calculateDistances(request) {
   service.getDistanceMatrix({
        origins: [origin],
        destinations: destinations[request],
        travelMode: google.maps.TravelMode.DRIVING,
        unitSystem: google.maps.UnitSystem.IMPERIAL,
        avoidHighways: false,
        avoidTolls: false
      }, function (response, status) {
    if (status != google.maps.DistanceMatrixStatus.OK) {
      alert('Error was: ' + status);
    } else {
      var origins = response.originAddresses;
      var destinationAdds = response.destinationAddresses;
      htmlString = '<table border="1">';
      deleteOverlays();
      for (var i = 0; i < origins.length; i++) {
        var results = response.rows[i].elements;
        for (var j = 0; j < results.length; j++) {
          htmlString += '<tr><td>'+destinationAdds[j]+'</td><td>' + results[j].distance.text +'</td></tr>';
        }
      }
    }
    var outputDiv = document.getElementById('outputDiv');
    htmlString += '</table>';
    outputDiv.innerHTML = htmlString;
  });
}

您需要查询FusionTable中的位置,并将查询中的位置用于距离矩阵。由于表中的行不到500行,我可能会使用google可视化库,但新的JSONPAPI也应该可以工作

距离矩阵限制为25个目的地。对于包含94行的表,概念验证比这更多的内容会遇到查询限制和配额问题

获取前25个结果的代码:

  // query the table for the destinations
  var queryString ="SELECT 'geometry' FROM "+FT_TableID;
  var queryText = encodeURIComponent(queryString);
  var query = new google.visualization.Query('http://www.google.com/fusiontables/gvizdata?tq='  + queryText);
  //set the callback function
  query.send(createDestinations);
}

function createDestinations(response) {
  if (!response) {
    alert('no response');
    return;
  }
  if (response.isError()) {
    alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
    return;
  } 
  FTresponse = response;
  //for more information on the response object, see the documentation
   //http://code.google.com/apis/visualization/documentation/reference.html#QueryResponse
  numRows = response.getDataTable().getNumberOfRows();
  numCols = response.getDataTable().getNumberOfColumns();
  var geoXml = new geoXML3.parser();
  var bounds = new google.maps.LatLngBounds();
  var request=0;
  destinations[0] = [];
  for (var i=0; ((i<numRows) && (i<25)); i++) {
    var kml = FTresponse.getDataTable().getValue(i,0);
    geoXml.parseKmlString("<Placemark>"+kml+"</Placemark>");
    destinations[request].push(geoXml.docs[i].markers[0].getPosition());
    bounds.extend(geoXml.docs[i].markers[0].getPosition());
  }
  map.fitBounds(bounds);
  calculateDistances(0);
}

function calculateDistances(request) {
   service.getDistanceMatrix({
        origins: [origin],
        destinations: destinations[request],
        travelMode: google.maps.TravelMode.DRIVING,
        unitSystem: google.maps.UnitSystem.IMPERIAL,
        avoidHighways: false,
        avoidTolls: false
      }, function (response, status) {
    if (status != google.maps.DistanceMatrixStatus.OK) {
      alert('Error was: ' + status);
    } else {
      var origins = response.originAddresses;
      var destinationAdds = response.destinationAddresses;
      htmlString = '<table border="1">';
      deleteOverlays();
      for (var i = 0; i < origins.length; i++) {
        var results = response.rows[i].elements;
        for (var j = 0; j < results.length; j++) {
          htmlString += '<tr><td>'+destinationAdds[j]+'</td><td>' + results[j].distance.text +'</td></tr>';
        }
      }
    }
    var outputDiv = document.getElementById('outputDiv');
    htmlString += '</table>';
    outputDiv.innerHTML = htmlString;
  });
}