无法阅读财产

时间:2014-10-16 18:34:32

标签: javascript jquery google-maps

这是我的剧本:

            if(entfernung2<=1 && drive=="AUT"){
                dauer = new google.maps.DistanceMatrixService();
                dauer.getDistanceMatrix({
                origins: ["48.142362,11.549859"],
                destinations: ["48.2,11.4"],
                travelMode: google.maps.TravelMode.WALKING,
                unitSystem: google.maps.UnitSystem.METRIC,
                })
            dauer1=dauer.rows.elements.duration.text;
            };

如果我在显示以下错误后记录dauer1:

未捕获的TypeError:无法读取属性&#39;元素&#39;未定义的 未捕获的TypeError:无法读取属性&#39; apply&#39;未定义的

什么错了? :/

编辑:

            if(entfernung2<=1 && drive=="AUT"){
                dauer = new google.maps.DistanceMatrixService();
            dauer.getDistanceMatrix(
            {
              origins: ["48.142362,11.549859"],
              destinations: ['"'+data.position.latitude+','+data.position.longitude+'"'],
              travelMode: google.maps.TravelMode.WALKING,
              unitSystem: google.maps.UnitSystem.METRIC
            }, setDauer1);

            function setDauer1(response, status) {
              dauer1 = response.rows[0].elements[0].duration.value;
              console.log(dauer1);
            }};

1 个答案:

答案 0 :(得分:0)

getDistanceMatrix是异步的,因此在dauer1更新之前定义dauer。通过查看docs,您需要回调:

dauer.getDistanceMatrix(
{
  origins: ["48.142362,11.549859"],
  destinations: ["48.2,11.4"],
  travelMode: google.maps.TravelMode.WALKING,
  unitSystem: google.maps.UnitSystem.METRIC
}, setDauer1);

function setDauer1(response, status) {
  dauer1 = response.rows[0].elements[0].duration.text;
  // your code continues here
}