我將一個字符串傳遞到頁面上的一個隱藏字段中,然後分割它以嘗試獲取可迭代的項目數組,並將多個標記添加到Google地圖上。如果我硬編碼數組,它的工作正常,但如果我嘗試拆分字符串並使用相同的代碼,它不起作用。我假設,因爲分割字符串創建一個字符串數組而不是數組對象。當我分割字符串時,數組以「['Smith Company',33.61678,-111.90017]」結尾,並用雙引號括起來。我將如何轉換這些項目,以便我可以像硬編碼值一樣使用它?將字符串數組值轉換爲數組對象
我在代碼後面的StringBuilder中添加方括號,單引號和垂直條。使用C#,如果它會有所作爲。我沒有這樣做,但仍然綠色與JavaScript,所以這是我最初的解決方案。我最初的字符串看起來像」
"['Jones Company', 26.16683, -98.23342]|['Smith Company', 33.61678, -111.90017]|['Bob Company', 29.70008, -98.03347]|['Blue Company', 37.71675, -122.21672]|['Red Company', 42.46692, -114.48342]"
感謝。
function initialize() {
var myOptions = {
zoom: 4,
center: new google.maps.LatLng(26.16683, -98.23342),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
//What I try to use when the hard coded is commented out
var myString = document.getElementById('hdnString').value;
var myItems = new Array();
myItems = myString.split("|");
//Hard coded items that I comment out when trying the above
var myItems = [
['Jones Company', 26.16683, -98.23342],
['Smith Company', 33.61678, -111.90017],
['Bob Company', 37.71675, -122.21672],
['Blue Company', 42.46692, -114.48342],
['Red Company', 36.58339, -121.8335]
];
// Add markers to the map
for (var i in myItems){
var myLatLng = new google.maps.LatLng(myItems[i][1], myItems[i][2]);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: myItems[i][0]
});
}
}
給我們你傳遞的隱藏字段完全匹配的字符串。 – Triptych 2009-06-23 19:54:11
我在後面的代碼中的StringBuilder中添加方括號,單引號和垂直條。使用C#,如果它會有所作爲。我沒有這樣做,但仍然綠色與JavaScript,所以這是我最初的解決方案。 ['Jones Company',26.16683,-98.23342] | ['Smith Company',33.61678,-111.90017] | ['Bob Company',29.70008,-98.03347] | ['Blue Company',37.71675,-122.21672] ['Red Company',42.46692,-114.48342] – 2009-06-23 19:58:17