I'm pulling an Address data from a Database to display on a FormView. I use a Javascript with a window.onload function that receives data from my code behind (longitude and Latitude). The formview is inside an UpdatePanel.
This works well for the ItemTemplate but the same map does not display in the EditTemplate. I'm assuming it has to do with the (getElementById("dvMap")) code I'm using which it can't find in the EditTemplate.
How code i make this code more efficient to display the map in either template?
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
I ended up using the asp.net GoogleMaps control located at [1]: http://en.googlemaps.subgurim.net/ and all my woes went away