Геометрическая библиотека в API карт Google предоставляет алгоритм, его можно найти в исходном коде .
Я не уверен, использует ли карта Google тот же алгоритм.
Алгоритм прост:
function toRadians(deg){
return deg * (Math.PI / 180);
}
function getDistance(from, to) {
var c = toRadians(from.lat()),
d = toRadians(to.lat());
return 2 * Math.asin(Math.sqrt(Math.pow(Math.sin((c - d) / 2), 2) + Math.cos(c) * Math.cos(d) * Math.pow(Math.sin((toRadians(from.lng()) - toRadians(to.lng())) / 2), 2))) * 6378137;
}
Эти две строки кодов будут иметь одинаковый результат:
console.log(google.maps.geometry.spherical.computeDistanceBetween(new google.maps.LatLng(39.915, 116.404), new google.maps.LatLng(38.8871, 113.3113)));
console.log(getDistance(new google.maps.LatLng(39.915, 116.404), new google.maps.LatLng(38.8871, 113.3113)));