簡體   English   中英

無法讀取屬性

[英]Cannot read property

這是我的腳本:

            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:無法讀取未定義的屬性“元素”未捕獲的TypeError:無法讀取未定義的屬性“ apply”

怎么了? :/

編輯:

            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);
            }};

getDistanceMatrix是異步的,因此在更新dauer之前先定義dauer1 通過查看文檔 ,您需要一個回調:

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM