gpt4 book ai didi

javascript - map 未加载到 HTML 页面

转载 作者:行者123 更新时间:2023-11-27 23:42:35 24 4
gpt4 key购买 nike

我有一张 map 想要加载到 HTML 页面中,该 map 的文件位于应用程序文件夹中,但未加载。这是插入 map 的位置的代码

<!-- Contact section start -->
<div id="contact" class="contact">
<div class="section ">
<div class="container">
<div class="title">
<h1>Contact Us</h1>
<p>Duis mollis placerat quam, eget laoreet tellus tempor eu. Quisque dapibus in purus in dignissim.</p>
</div>
</div>
<div class="map-wrapper">
<div class="map-canvas" id="map-canvas">Loading map...</div>
<div class="container">
<div class="row-fluid">
<div class="span5 contact-form centered">
<h3>Say Hello</h3>
<div id="successSend" class="alert alert-success invisible">
<strong>Well done!</strong>Your message has been sent.</div>
<div id="errorSend" class="alert alert-error invisible">There was an error.</div>
<form id="contact-form" action="php/mail.php">
<div class="control-group">
<div class="controls">
<input class="span12" type="text" id="name" name="name" placeholder="* Your name..." />
<div class="error left-align" id="err-name">Please enter name.</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<input class="span12" type="email" name="email" id="email" placeholder="* Your email..." />
<div class="error left-align" id="err-email">Please enter valid email adress.</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<textarea class="span12" name="comment" id="comment" placeholder="* Comments..."></textarea>
<div class="error left-align" id="err-comment">Please enter your comment.</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<button id="send-mail" class="message-btn">Send message</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="container">
<div class="span9 center contact-info">
<p style="color:black;">123 Fifth Avenue, 12th,Belgrade,SRB 11000</p>
<p class="info-mail">ourstudio@somemail.com</p>
<p style="color:black;">+11 234 567 890</p>
<p style="color:black;">+11 286 543 850</p>

</div>

</div>
</div>
</div>
<!-- Contact section edn -->

这是 js 文件,其中包含加载 map 所需的所有代码。

    //Initialize google map for contact setion with your location.

function initializeMap() {

var lat = '44.8164056'; //Set your latitude.
var lon = '20.46090424'; //Set your longitude.

var centerLon = lon - 0.0105;

var myOptions = {
scrollwheel: false,
draggable: false,
disableDefaultUI: true,
center: new google.maps.LatLng(lat, centerLon),
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

//Bind map to elemet with id map-canvas
var map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);
var marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(lat, lon),
});

var infowindow = new google.maps.InfoWindow();

google.maps.event.addListener(marker, 'click', function () {
infowindow.open(map, marker);
});

infowindow.open(map, marker);
google.maps.event.addDomListener(window, 'load', initializeMap);

}

我看到其他人也有类似的问题,但他们通过在标记区域中键入optimized:false 来修复它,但对我来说它仍然不起作用。

最佳答案

您有两个问题:

  1. 您调用 google.maps.event.addDomListener(window, 'load',initializeMap); 内部 initializeMap()本身 - 那么 map 自然不会被初始化。

  2. 您需要在#map-canvas上设置一些尺寸;即使问题 #1 不是这种情况, map 也永远不会显示。

当这两个问题得到纠正后,它就可以工作了 -> http://jsfiddle.net/1fdzz7gv/

fiddle 与OP中的代码完全相同,除了google.maps.event.addDomListener(window, 'load',initializeMap);放置在initializeMap()#map-canvas 已被指定一些尺寸

#map-canvas {
width: 300px;
height: 200px;
}

关于javascript - map 未加载到 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33539528/

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