我从数据库中提取地址数据以显示在FormView上。我使用带有window.onload函数的Javascript,它接收来自我的代码后面的数据(经度和纬度)。 formview位于UpdatePanel内。
这适用于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