将变量从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 (将#修改为@)