I am using ASP.NET MVC 4 and I have a DIV like this:
<div id="map_canvas" style="width: 500px; height: 400px;" onload="mapAddress();"></div>
Then, in a JavaScript file (that I’ve verified is loaded) is the mapAddress function:
function mapAddress() {
//In this case it gets the address from an element on the page, but obviously you could just pass it to the method instead
var address = $("#Address").val();
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var myLatLng = results[0].geometry.location.LatLng;
var mapOptions = {
center: myLatLng,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: $("#Name").val() + " Location"
});
}
else {
alert("The location of the event could not be mapped because: " + status);
}
});
}
But for whatever reason it’s not being called. Did I misunderstand the onload event?
Thanks all!
onloadwill not fire for a<div>element.You can do that for the
documentorbody, or the less desired way of calling the script immediately after the<div>.