我正在尝试将Google Maps DistanceMatrix api查找的结果转换为javascript数组,但我遇到的问题是我收到错误:
Uncaught ReferenceError: arrResults is not defined
我遇到问题的行是:
html += parseInt(elements[j].distance.text) + "<br />";
arrResults.push(elements[j].distance.text);
这是整个代码:
<script type="text/javascript">
$( document ).ready(function() {
$( "#submit" ).click(function() {
$("#my_map").gmap3({
getdistance:{
options:{
origins:["pe219px","ng323rj"],
destinations:["pe219px","ng323rj"],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.IMPERIAL
},
callback: function(results, status){
var html = "";
if (results){
for (var i = 0; i < results.rows.length; i++){
var elements = results.rows[i].elements;
for(var j=0; j<elements.length; j++){
switch(elements[j].status){
case "OK":
var sd=$(this).text();
html += parseInt(elements[j].distance.text) + "<br />";
arrResults.push(elements[j].distance.text);
break;
case "NOT_FOUND":
html += "The origin and/or destination of this pairing could not be geocoded<br />";
break;
case "ZERO_RESULTS":
html += "No route could be found between the origin and destination.<br />";
break;
}
}
}
} else {
html = "error";
}
$("#my_map").html( html );
}
}
});
}); });
</script>
答案 0 :(得分:2)
您缺少arrResults
的声明:
callback: function(results, status){
var html = "";
var arrResults = [];
...