有人可以帮我解决这个代码片段出了什么问题吗?我最终的目标是使用google javascript API查找距离两个位置.我已经使用geocomplete jquery函数进行地址自动完成.点击搜索没有任何事情发生.我只是一个初学者请帮忙
Distance between Two Places
Nouphal.M.. 8
您可以使用Google地图的DistanceMatrixService来计算两点之间的距离.
以下函数计算
function calculateDistances() { origin = document.getElementById('source').value; //Get the source string destination = document.getElementById('dest').value; //Get the destination string var service = new google.maps.DistanceMatrixService(); //initialize the distance service service.getDistanceMatrix( { origins: [origin], //set origin, you can specify multiple sources here destinations: [destination],//set destination, you can specify multiple destinations here travelMode: google.maps.TravelMode.DRIVING, //set the travelmode unitSystem: google.maps.UnitSystem.METRIC,//The unit system to use when displaying distance avoidHighways: false, avoidTolls: false }, calcDistance); // here calcDistance is the call back function }
回调函数calcDistance的代码
function calcDistance(response, status) { if (status != google.maps.DistanceMatrixStatus.OK) { // check if there is valid result alert('Error was: ' + status); } else { var origins = response.originAddresses; var destinations = response.destinationAddresses; deleteOverlays(); for (var i = 0; i < origins.length; i++) { var results = response.rows[i].elements; for (var j = 0; j < results.length; j++) { alert ('Distance from '+origins[i] + ' to ' + destinations[j] + ': ' + results[j].distance.text ); // alert the result } } } }
你可以在这里找到一个工作演示
您可以使用Google地图的DistanceMatrixService来计算两点之间的距离.
以下函数计算
function calculateDistances() { origin = document.getElementById('source').value; //Get the source string destination = document.getElementById('dest').value; //Get the destination string var service = new google.maps.DistanceMatrixService(); //initialize the distance service service.getDistanceMatrix( { origins: [origin], //set origin, you can specify multiple sources here destinations: [destination],//set destination, you can specify multiple destinations here travelMode: google.maps.TravelMode.DRIVING, //set the travelmode unitSystem: google.maps.UnitSystem.METRIC,//The unit system to use when displaying distance avoidHighways: false, avoidTolls: false }, calcDistance); // here calcDistance is the call back function }
回调函数calcDistance的代码
function calcDistance(response, status) { if (status != google.maps.DistanceMatrixStatus.OK) { // check if there is valid result alert('Error was: ' + status); } else { var origins = response.originAddresses; var destinations = response.destinationAddresses; deleteOverlays(); for (var i = 0; i < origins.length; i++) { var results = response.rows[i].elements; for (var j = 0; j < results.length; j++) { alert ('Distance from '+origins[i] + ' to ' + destinations[j] + ': ' + results[j].distance.text ); // alert the result } } } }
你可以在这里找到一个工作演示