Ich versuche Objekte auf ein Array mit dem folgenden Code hinzuzufügen:JS Hinzufügen von Objekt zu einem Array
for (i = 0; i < tweet_object.length; i ++) {
markers[i] = new Object;
markers[i] = {
title:tweet_object[i].title,
latitude:tweet_object[i].latitude,
longitude:tweet_object[i].longitude,
rating:tweet_object[i].importance
};
Ich habe var marker = []; oben am Anfang des Codes mit meinen globalen Variablen. Das Ziel ist, dass Marker [i] ein Objekt sind, auf das man leicht anderswo zugreifen kann. Ich habe Marker [i] innerhalb der Funktion getestet, und alle Werte sind erfolgreich. Wenn ich jedoch aus dieser Funktion herausgehe und versuche, irgendetwas einschließlich Markierungen [#] aufzurufen, wird mir gesagt, dass Marker [#] ist nicht definiert. (Ich habe sowohl mit als auch ohne die Marker [i] = new Object; line) versucht. Warum sollte das Array innerhalb seiner Funktion funktionieren, aber nicht außerhalb?
EDIT Klärung - das ist mein voller Code bis zu dem Abschnitt bereits gezeigt. Das Marker-Array wird außerhalb jeder Funktion deklariert und sollte (denke ich) global sein.
EDIT zur ursprünglichen Bearbeitung - das ist alles bis zu dem, wo ich versuche, Marker [#] zu verwenden, in der allerletzten Zeile. Es ist außerhalb jeder Funktion. Einige der Abstand wurde vermasselt - $ (function() {geht den ganzen Weg bis unter die „instanziiert Karte“ line Die console.log Aussage ist das erste, was außerhalb der Funktion
/*global json_tweet_data*/
/*global index*/
// Google Map
var map;
// markers for map
var markers = [];
// info window
var info = new google.maps.InfoWindow();
// execute when the DOM is fully loaded
$(function() {
function reqListener()
{
//console.log(this.responseText);
}
var oReq = new XMLHttpRequest(); //New request object
oReq.onload = function()
{
var json_tweet_data = this.responseText;
var tweet_object = JSON.parse(json_tweet_data);
//CREATE MARKERS FOR NEWS ITEMS
for (i = 0; i < tweet_object.length; i ++) {
//console.log("Latitude for " + i + ": " + tweet_object[i].latitude);
//console.log("Longitude for " + i + ": " + tweet_object[i].longitude);
markers[i] = {
title:tweet_object[i].title,
latitude:tweet_object[i].latitude,
longitude:tweet_object[i].longitude,
rating:tweet_object[i].importance
};
//console.log(i + ": " + JSON.stringify(markers[0]));
if (tweet_object[i].latitude !== 0 && tweet_object[i].longitude !== 0) {
var myLatLng = {lat: parseFloat(tweet_object[i].latitude), lng: parseFloat(tweet_object[i].longitude)};
//console.log(tweet_object[i].title);
//console.log("LatLng: " + myLatLng.lat + ", " + myLatLng.lng);
//console.log("Rating: " + tweet_object[i].importance);
if (tweet_object[i].importance <= 40) {
var marker = new google.maps.Circle({
strokeColor: '#0000FF',
strokeOpacity: .8,
strokeWeight: 2,
fillColor: '#0000FF',
fillOpacity: .4,
radius: 160000,
map: map,
center: myLatLng
});
}
else if ((tweet_object[i].importance <= 80) && (tweet_object[i].importance > 40)) {
var marker = new google.maps.Circle({
strokeColor: '#00FFFF',
strokeOpacity: .8,
strokeWeight: 2,
fillColor: '#00FFFF',
fillOpacity: .4,
radius: 160000,
map: map,
center: myLatLng
});
}
else if ((tweet_object[i].importance <= 120) && (tweet_object[i].importance > 80)) {
var marker = new google.maps.Circle({
strokeColor: '#00FF00',
strokeOpacity: .8,
strokeWeight: 2,
fillColor: '#00FF00',
fillOpacity: .4,
radius: 160000,
map: map,
center: myLatLng
});
}
else if ((tweet_object[i].importance <= 160) && (tweet_object[i].importance > 120)) {
var marker = new google.maps.Circle({
strokeColor: '#00FF66',
strokeOpacity: .8,
strokeWeight: 2,
fillColor: '#00FF66',
fillOpacity: .4,
radius: 160000,
map: map,
center: myLatLng
});
}
else if (tweet_object[i].importance > 160) {
var marker = new google.maps.Circle({
strokeColor: '#FF0000',
strokeOpacity: .8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: .4,
radius: 160000,
map: map,
center: myLatLng
});
}
};
}
};
oReq.open("get", "variables_for_js.php", true);
oReq.send();
// options for map
// https://developers.google.com/maps/documentation/javascript/reference#MapOptions
var options = {
center: {lat: 39.8282, lng: -98.5795}, // Geographic center of contiguous 48 US states
disableDefaultUI: true,
mapTypeId: google.maps.MapTypeId.ROADMAP,
maxZoom: 14,
panControl: true,
styles: styles,
zoom: 2,
zoomControl: true
};
// get DOM node in which map will be instantiated
var canvas = $("#map-canvas").get(0);
// instantiate map
map = new google.maps.Map(canvas, options);
});
console.log("Testing: " + JSON.stringify(markers[0]));
', wenn ich hier herauskommen function', was diese Funktion ist? –
Sie Sind sicher, dass es in der Struktur hoch genug definiert ist, um wirklich global zu sein? Klingt so, als hätten Sie es mit einem Oszilloskop-Problem zu tun, obwohl Sie versucht haben, dafür zu planen. – ThrowBackDewd
Sie brauchen die Zeile 'markers [i] = new Object;' - die nachfolgende Zeile erzeugt ein neues Objekt und löscht trotzdem das ursprüngliche Objekt aus. – Pointy