将服务器端循环变量分配给客户端数组



更新先生,这是我网页的当前代码。我根据您的建议进行了更改,但标题不可见或未显示我错过的内容

  <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()
      %> 

更新2JS代码在这里

  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]);
        });

}    

先生我的信息窗口没有打开什么可能的问题

这取决于

您在 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数据

最新更新