gpt4 book ai didi

javascript - 如何在 ASP.NET C# 中显示谷歌地图

转载 作者:太空宇宙 更新时间:2023-11-04 15:42:17 24 4
gpt4 key购买 nike

我正在努力将 google map 控件添加到 ASP.NET C# 页面中。我已经从谷歌获得了 API key ,我刚刚测试了该页面,但似乎 map 没有显示。我的最终结果是使用纬度和经度的文本框进行输入,单击提交按钮后, map 会将我带到指定的位置。在前端,我有这个:

<script>
var map;
function initialize() {
map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(48.1293954, 11.556663), // Munich Germany
zoom: 10
});
}

function newLocation(newLat, newLng) {
map.setCenter({
lat: newLat,
lng: newLng
});
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>
<form id="form1" runat="server">
<asp:ScriptManager ID="sManager" runat="server" />
<asp:UpdatePanel ID="pnlTest" runat="server">
<ContentTemplate>
<div style="height: 60%;" id="map"></div>

<asp:TextBox ID="txtLat" runat="server" />
<asp:TextBox ID="txtLong" runat="server" />
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</form>

我还没有完成按钮单击事件,因为我想先完成以上内容。

最佳答案

问题是渲染 map 时您的 UpdatePanel 没有高度。如果您为控件指定高度,则 map 将可见:

<asp:UpdatePanel ID="pnlTest" runat="server" style="height:400px;">

Google documentation表示您必须明确设置高度:

Note that divs usually take their width from their containing element, and empty divs usually have 0 height. For this reason, you must always set a height on the div explicitly.

关于javascript - 如何在 ASP.NET C# 中显示谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43782032/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com