我目前正在使用Google Maps Javascript API来显示客户的所有位置。它仅适用于PHP,但现在我正在使用$ .ajax()函数。
function init() {
map = new google.maps.Map(document.getElementById('googleMap'), {
zoom: 6,
center: {lng: 10.1293945, lat: 51.1793430}
});
var markers = addMarkers('', '', 'POST');
return map;
}
function addMarkers(f, s, method) {
console.log("Filter: " + f);
$.ajax({
url: 'user-load.php' + "?echo=1&method=" + method + "&filter=" + f + "&search=" + s,
dataType: 'json',
success: function(data) {
var count = data['length'];
console.log(count + " found");
var displayed = 0;
for(var i = 0; i < count; i++) {
if(data[i]['lat'] != 0 && data[i]['lat'] != 0) {
displayed++;
var name = data[i]['first'] + " " + data[i]['last'];
var url = '';
if(data[i]['url'] != '') {
url = "<p>Website: <a target=\x22_blank\x22 href=\x22" + data[i]['url'] + "\x22>" + data[i]['url'] + "</a></p>";
}
var mail = '';
if(data[i]['mail'] != '') {
url = "<p>E-Mail: " + data[i]['mail'] + "</p>";
}
var marker = new google.maps.Marker({
position:{lng: parseInt(data[i]['lng']), lat: parseInt(data[i]['lat'])},
map: map,
title: name + ", " + data[i]['title'],
animation: google.maps.Animation.DROP
});
var c = '<h1>' + name + '</h1>' +
'<h4>' + data[i]['title'] + '</h4>' +
'<p>' + data[i]['Land'] + ", " + data[i]['PLZ'] + " " + data[i]['Ort'] + '</p>' +
'<p>' + data[i]['Strasse'] + '</p>' +
mail +
url;
var window = new google.maps.InfoWindow({
content: c
});
marker.addListener('click', (function(map,marker, window){
return function() {
window.open(map, marker);
};
})(map, marker, window));
markers.push(marker);
} else {
console.log("Not valid: " + data[i]);
}
}
console.log(displayed + " displayed");
},
error: function(data) {
console.log("ERROR!");
}
});
}
所以这是我的整个代码。我的user-load.php的输出数组没有错误。效果很好,结果 - &gt; num_rows等于标记数组长度,但557中只有约50个标记。问题是什么?我是JS的初学者,一开始有点混乱。
答案 0 :(得分:1)
固定它。 这样一个愚蠢的错误,搜索了5个小时。 而不是
parseInt(data[i]['lng'])
这样:
parseFloat(data[i]['lng'])
现在一切顺利。也许我可以帮助别人:)