我有兩個Javascript數組已被forloop填充。我現在想把這兩個數組分配給一個新的數組。但是,這兩個數組在另一個函數中被調用。所以,我想將包含兩個數組的函數分配給一個新數組。包含數組的Javascript數組
這個想法是使用while循環來填充兩個現有數組的新數組。
的困難是,瀏覽器似乎不喜歡設定陣列的google.maps功能:例如:myarray[i] = (google.maps.LatLng(array1[i],array2[i]))
這裏是整個代碼...這是怎麼了?實際位包含內代碼結束時的while循環(while循環之前的其餘部分工作正常)。
while($row = mysqli_fetch_array($marker_result)) {
$marker_location[] = $row["location"];
$marker_lat[] = $row["lat"];
$marker_lng[] = $row["lng"];
} //End of while loop
?>
<script>
//Converting php marker arrays to javascript marker arrays for use in for loop
var js_markerloc_array= <?php echo json_encode($marker_location); ?>;
var js_markerlat_array= <?php echo json_encode($marker_lat); ?>;
var js_markerlng_array= <?php echo json_encode($marker_lng); ?>;
var markerloc_array = new Array(js_markerloc_array.length);
var markerlat_array = new Array(js_markerloc_array.length);
var markerlng_array = new Array(js_markerloc_array.length);
for(var i=0; i<js_markerloc_array.length; i++){
var jsloc = js_markerloc_array[i];
var jslat = js_markerlat_array[i];
var jslng = js_markerlng_array[i];
markerloc_array[i] = jsloc;
markerlat_array[i] = jslat;
markerlng_array[i] = jslng;
}
// Now need to write all of this below into an array so that it can populate markers from it! The difficulty is that the browser does not seem to like assigning the google.maps.LatLng(array1[i],array2[i]) to a new array
//var mapmarker_array = new Array(js_markerloc_array.length);
i = 0;
while (i < js_markerloc_array.length)
{
var mapmarker_array = new (google.maps.LatLng(markerlat_array[i], markerlng_array[i])); //Uses coordinates from database "markers" table
var test_marker1 = new google.maps.Marker({position:mapmarker1, title: markerloc_array[i]});
document.write(mapmarker1);
document.write(markerloc_array[i]);
i++;
}
你在頂部的forloop似乎有點毫無意義。你有三個數組,你循環,創建三個與前三個數組相同的數組......剛使用三個原始數組有什麼不對? –
你的大部分JS似乎毫無意義。你在PHP中創建數組,然後用json_encode將它們嵌入到JS中,然後循環這些數組並將它們的內容複製到OTHER數組中,然後循環這些新數組並嘗試執行谷歌的東西。所有陣列擺弄的是什麼?您只需生成適當的數組名稱即可。 –