我想添加一个谷歌地图到一个ASP .Net子页面(它有一个母版页)的div。地图信息似乎从谷歌加载 - 但我什么都看不到。当我在Chrome中打开F12时,我可以看到我的div已经充满了很多其他的div和关于地图的数据。ASP.net谷歌地图
任何想法为什么我的地图是不可见的?
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Contact.aspx.cs" Inherits="CentralWebsite.Contact" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<style type="text/css">
.style1
{
width: 317px;
}
</style>
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?key=mykey=false">
</script>
<script type="text/javascript">
function initialize() {
var myOptions = {
center: new google.maps.LatLng(-34.397, 150.644),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
alert("alert " + document.getElementById("map_canvas"));
}
</script>
</asp:Content>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<table style="width: 100%;">
<tr>
<td class="style1">
<h1>Telephone: 01709</h1>
<h1>Address</h1>
<h3></h3>
<h3>Rotherham</h3>
<h3>South Yorkshire</h3>
<h3>S60 1PP</h3>
</td>
<td>
<div id="map_canvas" style="width:100%; height:100%">
map should go here
</div>
</td>
</tr>
</table>
</asp:Content>
我通过如下将其作为母版页身体的onload属性调用初始化函数:
public partial class Contact : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HtmlGenericControl myBody = (HtmlGenericControl)Master.FindControl("myBody");
myBody.Attributes.Add("onload", "initialize()");
}
}
非常感谢
罗布。
您必须在起始页面调用JavaScript方法initialize()。然后将工作! – 2012-01-15 10:38:29
对不起,我没有解释 - 我正在从子页面的page_load调用初始化函数。我知道正在调用initialize,因为Alert对话框显示 – 2012-01-15 14:49:43