Javascript 查找两次调用地理代码创建的两个latlng对象之间的距离

Javascript 查找两次调用地理代码创建的两个latlng对象之间的距离,javascript,google-maps-api-3,Javascript,Google Maps Api 3,我在代码中创建了两个latlng对象,由于异步请求,这两个值中的任何一个在被调用后都不可用 我怎样才能找到这两点之间的距离 我已经找到了查找距离的代码,只是无法获得那些latlng值 代码如下: function onselectchange(){ var address = document.getElementById("start").value; var address2 = document.getElementById("end").value; var startLat, st

我在代码中创建了两个latlng对象,由于异步请求,这两个值中的任何一个在被调用后都不可用

我怎样才能找到这两点之间的距离

我已经找到了查找距离的代码,只是无法获得那些latlng值

代码如下:

function onselectchange(){
var address = document.getElementById("start").value;

var address2 = document.getElementById("end").value;

var startLat, startLong, endLat, endLong;
var Firstlatlng , Secondlatlng;
geocoder.geocode( { 'address': address}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {

  startLat = results[0].geometry.location.lat();
  startLong = results[0].geometry.location.lng();

  Firstlatlng = new google.maps.LatLng(results[0].geometry.location.lat(),results[0].geometry.location.lng());
    //alert("Inside:"+startLat);



  } 

  else {
    alert("Geocode was not successful for the following reason: " + status);
  }
});

geocoder.geocode( { 'address': address2}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {

  endLat = results[0].geometry.location.lat();
  endLong = results[0].geometry.location.lng();
  Secondlatlng = new google.maps.LatLng(results[0].geometry.location.lat(),results[0].geometry.location.lng());

  } 

  else {
    alert("Geocode was not successful for the following reason: " + status);
  }
});
}

function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
  var R = 6371; // Radius of the earth in km
  var dLat = deg2rad(lat2-lat1);  // deg2rad below
  var dLon = deg2rad(lon2-lon1); 
  var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2)
    ; 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var d = R * c; // Distance in km
  return d;
}

function deg2rad(deg) {
  return deg * (Math.PI/180)
}
一些帮助将不胜感激


提前感谢:D

您可以将板条推入阵列,当阵列长度为2时,计算距离

简单的实现(使用几何体库的方法),它还存储地理编码后的latlngs(无需再次请求地址)

var geocoder=new google.maps.geocoder();
函数onselectchange(){
var latLngs=[],
计算=函数(){
如果(latLngs.length==2){
var distance=Math.round(google.maps.geometry.spherical
.ComputedDistanceBetween(latLngs[0],
latLngs[1]);
打印(距离);
}
};
['start','end'].forEach(函数(id){
var list=document.getElementById(id),
option=list.options[list.selectedIndex];
如果(选项latlng){
latLngs.push(选项latLngs);
}否则{
地理编码({
地址:option.value
},
功能(结果、状态){
if(status==google.maps.GeocoderStatus.OK){
var latLng=results[0]。geometry.location
option.latlng=latlng;
车推(latLng);
计算();
}
}
);
}
}),
打印=功能(距离){
document.getElementById('result')。textContent=
(距离)?数学圆(距离/1000)+“公里”:“;
}
打印();
计算();
}

柏林
罗马
巴黎
东京
芝加哥
柏林
罗马
巴黎
东京
芝加哥

这里是我根据评论中给出的提示所做的

var startLat, startLong, endLat, endLong;
var Firstlatlng , Secondlatlng;
geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {

        startLat = results[0].geometry.location.lat();
        startLong = results[0].geometry.location.lng();

        Firstlatlng = new google.maps.LatLng(results[0].geometry.location.lat(),results[0].geometry.location.lng());
        //alert("Inside:"+startLat);
        geocoder.geocode( { 'address': address2}, function(results2, status2) {
            if (status2 == google.maps.GeocoderStatus.OK) {

                endLat = results2[0].geometry.location.lat();
                endLong = results2[0].geometry.location.lng();
                Secondlatlng = new google.maps.LatLng(results2[0].geometry.location.lat(),results2[0].geometry.location.lng());

                //alert(startLat+" "+startLong+" "+endLat+" "+endLong);
                var R = 6371; // Radius of the earth in km
                var dLat = deg2rad(endLat-startLat);  // deg2rad below
                var dLon = deg2rad(endLong-startLong); 
                var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(deg2rad(startLat)) * Math.cos(deg2rad(endLat)) 
                * Math.sin(dLon/2) * Math.sin(dLon/2); 
                var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
                var d = R * c; // Distance in km
                var firstSelect = document.getElementById('start');
                var secondSelect = document.getElementById('end');


                //return elt.options[elt.selectedIndex].text;
                alert("The distance between "+firstSelect.options[firstSelect.selectedIndex].text + " and " + secondSelect.options[secondSelect.selectedIndex].text + " is " +Math.round(Number(d)) +" Km");
                //alert(d);  

            } 

            else {
                alert("Geocode was not successful for the following reason: " + status2);
            }
        });

  } 

  else {
    alert("Geocode was not successful for the following reason: " + status);
  }
});

谢谢大家的帮助

Google Maps Api提供了一个查找两个LatLng点之间距离的函数:
ComputedIstanceBeween(a,B)
More info here:quick fix.将一个地理代码调用嵌套在另一个的回调中,并在第二个的回调中处理距离。。。或者…集成承诺,以便两个调用都可以进行,并且在两个承诺都解决之前不会发生CALC。我知道这个函数,但为此,我需要找到两次调用的LatLng点,但由于异步行为,它的值仅在该调用中绑定。有什么想法吗?@charlietfl-ohh!!这似乎是个好办法!我会尝试很多,这里是另一个很好的例子。谢谢。甚至我也有办法找到距离!在第一次呼叫中呼叫!