【发布时间】:2015-10-24 14:22:49
【问题描述】:
您好,我有一个 json 对象,返回如下;
{
"status": "ok",
"data": {
"mapdata": [
{
"name": "Test title",
"description": "Lorem ipsum dolor sit amet",
"lat": "51.297387",
"lng": "-0.737240"
}
{
"name": "Example Event 5",
"description": "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam eget tincidunt turpis. Nam tincidunt non diam eu mollis. Suspendisse lobortis rhoncus ante at auctor. </p>\r\n",
"lat": "51.5232032",
"lng": "-0.6053448999999773"
}
]
}
}
我想遍历数据数组,但我不知道如何,这是我目前的代码:
$.ajax({
url: '/googlemaps',
type: 'POST',
dataType: 'json',
success: function (response) {
for (var key in response.data) {
console.log(key.name);
var latLng = new google.maps.LatLng(key.lat, key.lng);
// Creating a marker and putting it on the map
var marker = new google.maps.Marker({
position: latLng,
map: map,
title: data.name
});
//send details to infoBox function to create infoWindow
infoBox(map, marker, data);
} // end for
} //end success
})
但是,当我遍历键并尝试将 lat 和 lng 传递给 google maps 函数时,它会输出此错误:
Uncaught TypeError: Cannot read property 'lat' of undefined
并且 console.log 未定义 console.log(key.name);。有什么想法我做错了什么以及如何循环遍历 mapdata 数组?
【问题讨论】:
标签: jquery json object google-maps-api-3