0

我刚刚设法在我的地图上显示Google Fusion表格中的标记。现在我想知道从用户指定点到存储在表格中的标记的距离。我想过为此使用距离矩阵。 https://developers.google.com/maps/documentation/javascript/distancematrix的代码示例对我来说工作得很好,但是,我不知道如何将表格中的标记定义为距离矩阵函数中的目标。Google maps距离矩阵和融合表格

如上所述,我现在需要我的变量来调用Fusion Table中的标记作为目标,而不是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) { 
} 

我将非常高兴,如果有人可以帮助我。我想应该有一些非常直接的解决方案,但我只是不明白:/

无论如何,非常感谢任何帮助!

回答

3

您需要在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; 
    }); 
} 

working example that gets the first 25 results

+0

首先,感谢您的帮助! – user1546800 2012-07-24 21:04:42