如何使用 node.js 查找两个地理点(一组经度和经度)之间的距离。
我有使用谷歌地图距离矩阵服务的客户端javascript代码。
我想在服务器端javascript(在 node.js router.js 或 datamodel.js 中)做同样的事情。
我的客户端 Javascript 代码:
function calculateDistances() {
var gs =require('googlemaps');
var latlong = { 'latitude': 52.5112, 'longitude': 13.45155};
var origin1 = gs.LatLng(13.125511084202,80.029651635576);
var origin2 = new google.maps.LatLng(12.9898593006481,80.2497744326417);;
var destinationA = new google.maps.LatLng(13.125511084202,80.029651635576);;
var destinationB = new google.maps.LatLng(12.9898593006481,80.2497744326417);
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [origin1, origin2],
destinations: [destinationA, destinationB],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, callback);
}
function callback(response, status) {
if (status != google.maps.DistanceMatrixStatus.OK) {
alert('Error was: ' + status);
} else {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
var outputDiv = '';
deleteOverlays();
for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
addMarker(origins[i], false);
for (var j = 0; j < results.length; j++) {
addMarker(destinations[j], true);
outputDiv += origins[i] + ' to ' + destinations[j]
+ ': ' + results[j].distance.text + ' in '
+ results[j].duration.text + '';
}
}
console.log(outputDiv);
}
}
你能建议我用距离矩阵示例代码在 node.js 包中做同样的事情吗?