2012-05-16 98 views
0

我正在開發一個ASP.NET項目,並且正在使用Google Maps。我在頁面加載時加載地圖。然後通過點擊一個按鈕我想添加一些標記。我正在使用下面的代碼。ASP.NET和谷歌地圖。點擊按鈕時添加標記

function LoadSecurity(locationList, message) { 
    if (document.getElementById("map_canvas") != null && 
     document.getElementById("map_canvas") != "null") { 

     map = new google.maps.Map(document.getElementById("map_canvas"), 
      myOptions); 

     for (var i = 0; i < locationList.length; i++) { 
      if ((i == 0) || (i == locationList.length - 1)) { 
       var args = locationList[i].split(","); 
       var location = new google.maps.LatLng(args[0], args[1]) 
       var marker = new google.maps.Marker({ 
        position: location, 
        map: map 
       }); 
       marker.setTitle(message[i]); 
      } 
     } 
    } 
} 

我用下面的代碼調用按鈕上的函數。

<asp:Button ID="Button1" runat="server" 
    OnClientClick="javascript: LoadSecurity('57.700034,11.930706','test')" 
    Text="Button" /> 

但是不起作用。請幫忙嗎?

+0

什麼是「不工作」其實*平均*?怎麼了? –

+0

我的意思是沒有出現標記 – user1292656

+0

你是否能夠做你正在嘗試的? – bjornruysen

回答

3

你問這裏的地方'我'的元素,但你的locationList只是在那一點上的字符串?

locationList[i].split(","); 

嘗試將其更改爲

locationList.split(","); 

但仍有一些別的奇怪的事情在你的代碼.. 您的循環從0到locationList的長度,但在這一點上locationList是一個字符串,而不是一個數組。所以,你的for循環,從0到19去...

如果你想一下就從你身上該字符串2個座標,看看下面的jsfiddle:http://jsfiddle.net/f3Lmh/

如果你試圖傳遞在該字符串中有多個座標,你必須稍微改變你傳遞它們的方式,所以很容易看到這些座標屬於誰。 我準備了另一個小jsfiddle:http://jsfiddle.net/f3Lmh/4/

+1

只需爲OP添加一個小提示:如果您需要傳入''「57.700034」,「11.930706」]或在'for'循環之前執行'split(',')'座標作爲一個字符串。 – peirix

+0

我在循環之前做了拆分,但它又不起作用 – user1292656

+0

也許在jsfiddle中顯示你正在嘗試的是什麼? – bjornruysen