![](/img/trans.png)
[英]Node.js api - Can we get accuracy distance between two latlong as per google map giving result in distance?
[英]How to find a distance between two geopoints in node.js?
如何使用node.js找到兩個地理點之間的距離(經度和經度集)
我使用谷歌地圖距離矩陣服務在客戶端javascript中的代碼。
我想在serversidejavascript(在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包中使用距離矩陣示例代碼做同樣的事情。
也許這個套餐? https://github.com/manuelbieh/geolib
而這種方法:
geolib.getDistance(object start, object end, [int accuracy])
查看https://github.com/edwlook/node-google-distance
var distance = require('google-distance');
distance.get(
{
origin: 'San Francisco, CA',
destination: 'San Diego, CA'
},
function(err, data) {
if (err) return console.log(err);
console.log(data);
});
這將輸出:
{
index: null,
distance: '807 km',
distanceValue: 807366,
duration: '7 hours 30 mins',
durationValue: 26981,
origin: 'San Francisco, CA, USA',
destination: 'San Diego, CA, USA',
mode: 'driving',
units: 'metric',
language: 'en',
avoid: null,
sensor: false
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.