我使用Google Maps Complex Markers示例将标记放在Json请求的某些位置。但是我收到了此消息:
未处理的承诺拒绝:TypeError:未定义不是对象(正在评估“ beaches.length”)
我不清楚为什么不加载JSON。 我是Javascript / JQuery的新手,所以需要帮助。
最初,变量 beaches 被馈入initMap()内部:
var beaches = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1]
];
我更改为使用iniMap()函数外部的loadJSON调用获取这些数据。
var beaches;
function setup() {
loadJSON("getData.php", gotData, 'jsonp');
}
function gotData(data) {
beaches = data;
}
如您所见,出于安全原因,我使用了“ jsonp”。我试图清除它,但问题仍然存在。
要显示数据,请执行此调用。该问题出现在“ beaches.lenght”中:
for (var i = 0; i < beaches.length; i++) {
var beach = beaches[i];
var marker = new google.maps.Marker({
position: {lat: beach[1], lng: beach[2]},
map: map,
shape: shape,
title: beach[0],
zIndex: beach[3]
});
}
如果有任何帮助,我将不胜感激...
答案 0 :(得分:-1)
已编辑
对于变量 beaches ,您必须使用JSON格式。 read this
将海滩转换为此:
var beaches =
[{
"title" : "Bondi Beach",
"lat" : -33.890542,
"lng" : 151.274856,
"zindex" : 4
},...]
还:
您必须使用google.maps.LatLng
对象作为 marker 对象
var pos = new google.maps.LatLng(beach.lat, beach.lng);
var marker = new google.maps.Marker({
position : pos ,
map: map,
shape: shape,
title: beach.title,
zIndex: beach.zindex
})