传递googlemaps时javascript NaN错误?

时间:2017-02-01 16:51:58

标签: javascript

我有从2个不同位置输入邮政编码后获取地址的代码。然后,我只想计算我的代码工作的距离,但我不能将VARS传递给提交?它在我手动输入时有效。

代码是: -

    <script>
$('#submit').click(function(){  

  //Get Postcode
  var number = $('#number').val();
  var postcode = $('#postcode').val().toUpperCase();;

  //Get latitude & longitude
  $.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address=' + postcode + '&sensor=false',  
            function(data) {
              var lat = data.results[0].geometry.location.lat;
              var lat1 = lat;
              var lng = data.results[0].geometry.location.lng;
              var lng1 = lng;
              var dist1 = lat1 + ',' +lng1;

  //Get address    
  $.getJSON('http://maps.googleapis.com/maps/api/geocode/json?latlng=' + lat + ',' + lng + '&sensor=false',  
            function(data) {              
              var address = data.results[0].address_components;              
              var street = address[1].long_name;
              var town = address[2].long_name;
              var county = address[3].long_name;                        

  //Insert
  $('#text').text(number + ', ' + street + ', ' + town + ', ' + county + ', ' + postcode);
 document.getElementById("distance1").innerHTML = lat1 + ',' +lng1;
 document.getElementById("miles").innerHTML = 'Enter end address';
    });
  });
});
</script>


<script>
$('#submit1').click(function(){  

  //Get Postcode
  var number1 = $('#number1').val();
  var postcode1 = $('#postcode1').val().toUpperCase();;

  //Get latitude & longitude
  $.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address=' + postcode1 + '&sensor=false',  
            function(data) {
              var lat = data.results[0].geometry.location.lat;
              var lat2 = lat;
              var lng = data.results[0].geometry.location.lng;
              var lng2 = lng;
              var dist2 = lat2 + ',' +lng2;


  //Get address    
  $.getJSON('http://maps.googleapis.com/maps/api/geocode/json?latlng=' + lat + ',' + lng + '&sensor=false',  
            function(data) {              
              var address = data.results[0].address_components;              
              var street = address[1].long_name;
              var town = address[2].long_name;
              var county = address[3].long_name;                        

  //Insert
  $('#text1').text(number1 + ', ' + street + ', ' + town + ', ' + county + ', ' + postcode1);
 document.getElementById("distance2").innerHTML = lat2 + ',' +lng2;

  document.getElementById("miles").innerHTML = 'Now calculate';

    });
  });
});
</script>

<script>
$('#submit2').click(function(){  

  document.getElementById("miles").innerHTML = 'Here it is';

var p1 = new google.maps.LatLng('dist1');
var p2 = new google.maps.LatLng('dist2');

alert(calcDistance(p1, p2));

//calculates distance between two points in km's
function calcDistance(p1, p2) {
  return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
}


});

</script>

0 个答案:

没有答案