我正在使用 ASP.NET MVC 4,我有一个这样的 DIV:
<div id="map_canvas" style="width: 500px; height: 400px;" onload="mapAddress();"></div>
然后,在一个 JavaScript 文件(我已验证已加载)中是mapAddress
函数:
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);
}
});
}
但无论出于何种原因,它都没有被调用。我误解了这个onload
事件吗?
谢谢大家!