2016-11-01 5 views
0

私はGoogleマップ上の2点間の距離を計算するためのより良い方法を見つけようとしましたが、結果は期待通りでした。GoogleマップV3 2点間の距離 - 異なる値

例えば、私がここに見つけた2つの解決策:Calculate distance between two points in google maps V3は同様です(それはaprox 249.3424456 Kmを生成しますが、BacauとBucharest - Romania間の距離はaprox。300 Kmです)。

plnkrリンク:http://plnkr.co/edit/cv25C6pga0lla7xyBDRO?p=preview

または:

$(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; 
}); 

私はGoogleマップのWeb APIをhttp://maps.googleapis.com/maps/api/distancematrix/json?origins=46.5610058,26.9098054&destinations=44.391403,26.1157184でこれらの距離を計算してみてください、それは距離が良い値である299キロ(であることを教えて - Bacauとブカレストの間の距離 - ルーマニア - aprox。300 Km)。

ウェブサイトごとにアクセスするのではなく、ローカルで計算する方が便利ですが、「google.maps.geometry.spherical.computeDistanceBetween」関数またはHaversineの式を使用すると、間違った値が計算されるのはなぜですか?

+1

私の意見は、最初の2つの方法(computeDistanceBetweenと半正矢式は)それは、距離ストレートを計算することである(APIダッシュボードから距離行列を有効にする必要があります)陸路上の距離ではなく、空中にある。 – Doro

+0

[2点間の距離を計算する方法]の複製が可能です(http://stackoverflow.com/questions/14625505/method-to-calculate-distance-between-two-points) – geocodezip

+0

可能な複製距離[走行距離の計算方法PHPの2つの緯度と経度の間](http://stackoverflow.com/questions/38358823/how-to-calculate-driving-distance-between-two-latitude-and-longitudes-in-php) – geocodezip

答えて

1

ソリューションはdistancematrix APIを使用してローカルに計算することである。https://developers.google.com/maps/documentation/javascript/distancematrix

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]; 
      } 
     } 
    } 
} 
関連する問題