繁体   English   中英

Google绘制距离矩阵和融合表

[英]Google maps Distance Matrix and Fusion Tables

我只是设法在地图上可视化Google Fusion Table中的标记。 现在,我想知道从用户指定的点到表中存储的那些标记的距离。 我想到了为此使用距离矩阵。 https://developers.google.com/maps/documentation/javascript/distancematrix上的代码示例对我来说很好用,但是,我不知道如何将表中的标记定义为我的Distance Matrix函数中的目标。

如上所述,我现在需要我的变量,该变量调用Fusion Table中的标记作为目标,而不是destA和destB。

这是我的变量:

var schools = new google.maps.FusionTablesLayer({
  query: {
    select: 'geometry',
    from: '1mae334i-txYZFixePEiC7lgYYyi4w6qDN87XAyw'    
  },
}); 

这是google文档中的基本代码。

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中查询其中的位置,然后在DistanceMatrix的查询中使用这些位置。 由于表中的行数少于500,因此我可能会使用Google可视化库,但是新的JSONP API也应该可以使用。

DistanceMatrix仅限25个目的地。 具有94行的表的概念证明,这远远超过了问题(遇到查询限制和配额)

http://www.geocodezip.com/v3_SO_FusionTables_DistanceMatrix.html

代码以获取前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;
  });
}

获得前25个结果的工作示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM