简体   繁体   English

两点之间的Google Maps V3距离-不同的值

[英]Google Maps V3 distance between two points - different values

I tried to find a better way to calculate distance between two points on google maps, but the result it wasn't what I expected. 我试图找到一种更好的方法来计算谷歌地图上两点之间的距离,但是结果却不是我所期望的。

For example, those two solutions which I found here: Calculate distance between two points in google maps V3 are similarly (it produces aprox. 249.3424456 Km - but its wrong because distance between Bacau and Bucharest - Romania - are aprox. 300 Km). 例如,我在这里找到的这两个解决方案类似: 计算google maps V3两个点之间的距离是类似的(它产生aprox。249.3424456 Km-但由于Bacau和罗马尼亚布加勒斯特之间的距离是aprox。300 Km,所以是错误的)。

The plnkr link: http://plnkr.co/edit/cv25C6pga0lla7xyBDRO?p=preview plnkr链接: http ://plnkr.co/edit/cv25C6pga0lla7xyBDRO?p=preview

Or: 要么:

$( document ).ready(function() {
var myLatLng = new google.maps.LatLng({ lat: 46.5610058, lng: 26.9098054 });
var myLatLng2 = new google.maps.LatLng({ lat: 44.391403, lng: 26.1157184 });
var calc1 = google.maps.geometry.spherical.computeDistanceBetween(myLatLng, myLatLng2)/1000;
console.log('calc1 = ' + calc1);

var start = { lat: 46.5610058, lng: 26.9098054 };
var stop = { lat: 44.391403, lng: 26.1157184 };

var rad = function(x) {
    return x * Math.PI / 180;
};

var getDistance = function(p1, p2) {
    var R = 6378137; // Earth’s mean radius in meter
    var dLat = rad(p2.lat - p1.lat);
    var dLong = rad(p2.lng - p1.lng);
    var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
        Math.cos(rad(p1.lat)) * Math.cos(rad(p2.lat)) *
        Math.sin(dLong / 2) * Math.sin(dLong / 2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    var d = R * c;
    return d; // returns the distance in meter
};
var calc2 = getDistance(start, stop)/1000;
console.log('calc2 = ' + calc2);


document.getElementById("test").innerHTML = 'calc1 = ' + calc1 + '<br/>calc2 = ' + calc2;
});

When I try to calculate these distance with google maps web api http://maps.googleapis.com/maps/api/distancematrix/json?origins=46.5610058,26.9098054&destinations=44.391403,26.1157184 it tell me that the distance is 299 Km (which is the good value - distance between Bacau and Bucharest - Romania - are aprox. 300 Km). 当我尝试使用Google Maps Web API计算这些距离时, http: //maps.googleapis.com/maps/api/distancematrix/json?origins=46.5610058,26.9098054&destinations= 44.391403,26.1157184它告诉我该距离为299 Km(这是一个很好的值-Bacau和布加勒斯特之间的距离-罗马尼亚-约为300公里)。

I prefer to calculate locally rather than to access every time a website but why when I use 'google.maps.geometry.spherical.computeDistanceBetween' function or Haversine formula it calculate a wrong value? 我更喜欢在本地计算而不是每次访问一个网站,但是为什么当我使用“ google.maps.geometry.spherical.computeDistanceBetween”函数或Haversine公式时却计算出错误的值?

The solution is to compute locally using distancematrix api: https://developers.google.com/maps/documentation/javascript/distancematrix (Have to enable Distance Matrix from API Dashboard) 解决方案是使用distancematrix api在本地进行计算: https : //developers.google.com/maps/documentation/javascript/distancematrix (必须从API仪表板启用距离矩阵)

var from = new google.maps.LatLng(46.5610058, 26.9098054);
var fromName = 'Bacau';
var dest = new google.maps.LatLng(44.391403, 26.1157184);
var destName = 'Bucuresti';

var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
    {
        origins: [from, fromName],
        destinations: [destName, dest],
        travelMode: 'DRIVING'
    }, callback);

function callback(response, status) {
    if (status == 'OK') {
        var origins = response.originAddresses;
        var destinations = response.destinationAddresses;

        for (var i = 0; i < origins.length; i++) {
            var results = response.rows[i].elements;
            console.log(results);
            for (var j = 0; j < results.length; j++) {
                var element = results[j];
                var distance = element.distance.text;
                var duration = element.duration.text;
                var from = origins[i];
                var to = destinations[j];
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM