I'm coding something on php with javascript and something really strange is happening.
I have this code:
var lat = [];
var lng = [];
downloadUrl('db.php', function (data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName('marker');
Array.prototype.forEach.call(markers, function (markerElem) {
lat.push(markerElem.getAttribute('lat'));
lng.push(markerElem.getAttribute('lng'));
var icon = customLabel[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
label: icon.label
});
});
});
When I run console.log(lat)
or lng
I see that the array contains some entries (9 to be exact) but then I print console.log(lat.length)
and it sais that the length is 0.
But then if under that code I add some more entries manuallylat.push("new entry");
I see that is added to the previous content and the length changes to 1.....What is going on here? On the picture you can see after I added the new entry, the length sais that is 10 there, but the console reports 1. Any ideas?
------EDIT----
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(47.401775, 8.772933),
zoom: 5
});
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP or XML file
var lat = [];
var lng = [];
downloadUrl('db.php', function (data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName('marker');
Array.prototype.forEach.call(markers, function (markerElem) {
var id = markerElem.getAttribute('id');
var name = markerElem.getAttribute('name');
var address = markerElem.getAttribute('address');
var type = markerElem.getAttribute('type');
lat.push(markerElem.getAttribute('lat'));
lng.push(markerElem.getAttribute('lng'));
var infowincontent = document.createElement('div');
var strong = document.createElement('strong');
strong.textContent = name
infowincontent.appendChild(strong);
infowincontent.appendChild(document.createElement('br'));
var text = document.createElement('text');
text.textContent = address
infowincontent.appendChild(text);
var icon = customLabel[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
label: icon.label
});
marker.addListener('click', function () {
infoWindow.setContent(infowincontent);
infoWindow.open(map, marker);
});
});
});
lat.push("new entry");
console.log(lat);
}