2014-09-25 38 views
0

我从数据库中拉取地址数据以显示在FormView上。我使用了一个带有window.onload函数的Javascript,它从我的代码后面(经度和纬度)接收数据。 formview在UpdatePanel中。无法在FormView的Edittemplate中显示Google Map

这适用于ItemTemplate,但相同的地图不显示在EditTemplate中。我假设它与我在EditTemplate中找不到的(getElementById(“dvMap”))代码有关。

如何使代码更高效地在任一模板中显示地图?

Thx。

<script type="text/javascript"> 

    window.onload = function() { 
     var mapOptions = { 
      center: new google.maps.LatLng(markers[0].lat, markers[0].lng), 
      zoom: 17, 
      mapTypeId: google.maps.MapTypeId.SATELLITE 
     }; 
     var infoWindow = new google.maps.InfoWindow({ maxWidth: 200 }); 
     var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions); 
     for (i = 0; i < markers.length; i++) { 
      var data = markers[i] 
      var myLatlng = new google.maps.LatLng(data.lat, data.lng); 
      var marker = new google.maps.Marker({ 
       position: myLatlng, 
       map: map, 
       title: data.title 
      }); 
      (function (marker, data) { 
       google.maps.event.addListener(marker, "click", function (e) { 
        infoWindow.setContent(data.description); 
        infoWindow.open(map, marker); 
       }); 
      })(marker, data); 
     } 
    } 
</script> 

HTML 
<asp:Literal runat="server" ID="jsLiteral"></asp:Literal> 
<div id="dvMap" style="width: 100%; height: 400px"> </div> 

CodeBehind vb.Net 
     Dim jsLiteral As Literal = DirectCast(formviewRecord.FindControl("jsLiteral"), Literal) 
    If (_Record.Long IsNot Nothing) And (Not String.IsNullOrWhiteSpace(_Record.Long)) And (_Record.Lat IsNot Nothing) And (Not String.IsNullOrWhiteSpace(_Record.Lat)) Then 
     Dim js As New StringBuilder 
     js.Append("<script type='text/javascript'>") 
     js.Append("var markers = [") 
     js.Append(" {") 
     js.Append("title: 'test',") 
     js.Append("lat: " + _Record.Lat + ",") 
     js.Append("lng: " + _Record.Long + ",") 
     js.Append("description: 'description'") 
     js.Append(" }") 
     js.Append("];") 
     js.Append("</script>") 

     jsLiteral.Text = js.ToString 

    End If 

回答

相关问题