Tuesday 15 May 2012

javascript - Cannot read property -



javascript - Cannot read property -

this script:

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

if log dauer1 next errors shown:

uncaught typeerror: cannot read property 'elements' of undefined uncaught typeerror: cannot read property 'apply' of undefined

whats wrong? :/

edit:

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 asynchronous, dauer1 defined before dauer gets updated. looking @ docs, need callback:

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; // code continues here }

javascript jquery google-maps

No comments:

Post a Comment