将服务器端循环变量分配给客户端阵列

时间:2011-12-29 13:16:11

标签: javascript vb.net google-maps-api-3

更新 先生,这是我网页的当前代码。我根据你的建议进行了更改,但标题是隐形的,或者没有显示我错过的内容

  <script runat=server>

      Dim mgps As New Text.StringBuilder

      Public ReadOnly Property GPS() As String
          Get
              Return mgps.ToString

          End Get
      End Property
      Dim station As New Text.StringBuilder
      Public ReadOnly Property STA() As String
          Get
              Return station.ToString

          End Get
      End Property

  </script>  

  <%  Dim con As New OleDbConnection
      con = New OleDbConnection("Data Source=sml; User ID=sml; Password=sml; provider=OraOLEDB.Oracle")
      con.Open()
      Dim cmd As OleDbCommand = New OleDbCommand("Select STA_NAME, GPS_ONE from GPS", con)

      Dim ds As New DataSet
      Dim I As Long

      Dim da As New OleDbDataAdapter(cmd)
      da.Fill(ds, "GPS")
      mgps.AppendLine("[")
      For I = 0 To ds.Tables("GPS").Rows.Count - 1
          '   mgps.AppendLine("new google.maps.LatLng(" & ds.Tables("GPS").Rows(I).Item("GPS_ONE") & "),")
          mgps.AppendLine("{GPS:new google.maps.LatLng(" & ds.Tables("GPS").Rows(I).Item("GPS_ONE") & "), Sta_Name:'" & STA & "'},")
      Next I
      mgps.AppendLine("];")

      con.Close()
      %> 

UPDATE2 JS代码就在这里

  for(i=0; i<GPS.length; i++)
{

      var image = 'ico/no.png';
      var infowindow = new google.maps.InfoWindow();
      var ContentString = GPS[i].TITLE
      markers[i] = new google.maps.Marker(
      { 
       position: GPS[i].GPS,
       map: map,
       draggable:true,
       icon:image,
       title:GPS[i].TITLE

       });                       
        google.maps.event.addListener(markers[i], 'click', function() {
        infowindow.setContent(ContentString);
        infowindow.open(map,markers[i]);
        });


}    

Sir My infowindow未开启可能的问题

1 个答案:

答案 0 :(得分:0)

这取决于您在GPS表的GPS_ONE列中存储数据的方式,但如果您的for循环如下所示:

for(i=0; i<GPS.length-1; i++)
{

      var image = 'ico/no.png';
      markers[i] = new google.maps.Marker(
      { 
       position: GPS[i],
       map: map,
       draggable:true,
       icon:image,
       title: (i+1) + GPS //why is this here? do you have two variables with the same name or did you mean GPS[i]?
       });                             
}

,GPS_ONE列中的数据如下所示:

new google.maps.LatLng(31.204549793299,72.264183974237)

然后你写的应该是有效的。

编辑:根据您上面的示例,您似乎没有创建一个谷歌地图latlng对象数组。尝试更改vb中的循环,如下所示:

    For I = 0 To ds.Tables("GPS").Rows.Count - 1
        mgps.AppendLine("new google.maps.LatLng(" & ds.Tables("GPS").Rows(I).Item("GPS_ONE") & "),")
    Next I

编辑第二个问题: 您可以将所有内容存储在JSON对象中,所以

    For I = 0 To ds.Tables("GPS").Rows.Count - 1
        mgps.AppendLine("{GPS:new google.maps.LatLng(" & ds.Tables("GPS").Rows(I).Item("GPS_ONE") & "), Title:'" & titleColumnSourceHere & "'},")
    Next I

然后你可以像这样使用这个JSON对象:

for(i=0; i<GPS.length-1; i++)
{

      var image = 'ico/no.png';
      markers[i] = new google.maps.Marker(
      { 
       position: GPS[i].GPS,
       map: map,
       draggable:true,
       icon:image,
       title: GPS[i].Title
       });                             
}

显然我建议将GPS变量重命名为更合适的东西,因为它现在包含非gps数据