Mik*_*oud 2 javascript google-maps-api-3 asp.net-mvc-4
我正在使用ASP.NET MVC 4,我有一个像这样的DIV:
<div id="map_canvas" style="width: 500px; height: 400px;" onload="mapAddress();"></div>
Run Code Online (Sandbox Code Playgroud)
然后,在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);
}
});
}
Run Code Online (Sandbox Code Playgroud)
但无论出于什么原因,它都没有被召集.我误解了这onload件事吗?
谢谢大家!
Gab*_*abe 10
onload 不会开火 <div>元素.
您可以在document或body之后立即调用脚本,或者不太理想的方式执行此操作<div>.
<div id="map_canvas" style="width: 500px; height: 400px;"></div>
<script>
mapAddress();
</script>
Run Code Online (Sandbox Code Playgroud)