微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将变量从javascript函数传递给另一个函数

如何解决将变量从javascript函数传递给另一个函数

我有2个javascript函数,第一个从给定地址生成纬度/经度,第二个显示带有两点之间红线的google map,我面临的问题是当我尝试通过纬度时/ Lng从第一个函数到第二个函数总是给我“未定义”,请提供任何帮助或建议。

<script>      
      var locationDepart = 'Mohamed BOUDIAF Airport';
      var locationDestination = 'Rabah Bitat Airport';
          
      function geocode(location){
          var lat;
          var lng;
          axios.get('https://maps.googleapis.com/maps/api/geocode/json',{
             params:{
                 address: location,key:'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
             } 
          })
          .then(function(response){
                lat = (response.data.results[0].geometry.location.lat);
                lng = (response.data.results[0].geometry.location.lng);
          })
          .catch(function(error){
              console.log(error);
          }) 
          
          var geocodeReturns = [lat,lng]; 
          return geocodeReturns;
       }
      
      window.Depart = geocode(locationDepart);
      window.Destination = geocode(locationDestination);
      
      "use strict";
      function initMap() {
        const map = new google.maps.Map(document.getElementById("map"),{
          zoom: 8,center: {
            lat: window.Depart[0],lng: window.Depart[1]
          },});
        const flightPlanCoordinates = [
          {
            lat: window.Depart[0],{
            lat: window.Destination[0],lng: window.Destination[1]
          }
        ];
        const flightPath = new google.maps.polyline({
          path: flightPlanCoordinates,geodesic: true,strokeColor: "#FF0000",strokeOpacity: 1.0,strokeWeight: 2
        });
        flightPath.setMap(map);
      }
</script>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。