function updateScreenContent() {
app.map.mapObject.getVisibleRegion(function (bounds) {
app.ajaxUpdater = $.ajax({
type: "GET",
url: "myapi.com/chickens/",
data: {coordinates: bounds.toUrlValue()},
dataType: "JSON"
})
.done(function (mapItems, status) {
for (var i = 0; i < mapItems.length; i++) {
if (!matchFound) {
app.map.mapObject.addMarker({
position: new plugin.google.maps.LatLng(mapItems[i].latitude, mapItems[i].longitude),
icon: app.map.icons.stop
}, function (item) {
item.id = mapItems[i].id;
app.map.mapContent.push("PUSH ME INTO THE ARRAY!!!!!");
});
}
}
})
});
}
The above function is called every time the map becomes idle in our application, this could be as much as every 500 milliseconds or longer depending on when the user stops moving the map.
My problem is I can't seem to add any content to my mapContent array inside of the callback where the markers have been successfully added. I know that it's being called because I've put an alert in there and also the markers are successfully added to the map.
Whenever I check the length of mapContent it's always 0. It makes no sense at all! If I push to the array outside of the callback it works.
Any ideas?
EDIT:
addMarker is an asynchronous function. Could this be why?
app.map.mapContent.pushexecutes, the value ofiisn't a valid index because JavaScript doesn't (automatically) have block level scoping of variables. Essentially, when that function executes the value ofiis its final value of the loop, not the value it had whenaddMarkerwas actually called. That results in an error being thrown by the line above the push, and it never being called.