【发布时间】:2017-06-22 22:49:15
【问题描述】:
我在引导模式的同一页面上有两个谷歌地图我正在使用谷歌地图 google.maps.event.addDomListener(window, 'load', initMap); 加载多个地图,让我给你看我的代码
HTML
<li data-toggle="modal" data-target="#myModal"><a href="#">Contact</a></li>
<h1 data-toggle="modal" data-target="#myModal2">Free Evaluation</h1>
<div class="modal fade" id="myModal2" role="dialog">
<div class="modal-dialog modal-custom">
<!-- Modal content-->
<div class="modal-content ">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<div id="map2" class="map2">
</div>
</div>
</div>
</div>
</div>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog modal-custom">
<!-- Modal content-->
<div class="modal-content ">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<div id="map" class="map2">
</div>
</div>
</div>
</div>
</div>
Java 脚本
function initMap() {
var myLatLng = {
lat: 43.6222102,
lng: -79.6694881
};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
center: myLatLng
});
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
});
}
function initMap2() {
var myLatLng = {
lat: 43.6222102,
lng: -79.6694881
};
var map = new google.maps.Map(document.getElementById('map2'), {
zoom: 15,
center: myLatLng
});
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
});
}
google.maps.event.addDomListener(window, 'load', initMap);
google.maps.event.addDomListener(window, 'load', initMap2);
$('#myModal').on('shown.bs.modal', function () {
google.maps.event.trigger(map, "resize");
map.setCenter(new google.maps.LatLng(42.3605336, -72.6362989));
});
$('#myModal2').on('shown.bs.modal', function () {
google.maps.event.trigger(map, "resize");
map.setCenter(new google.maps.LatLng(42.3605336, -72.6362989));
});
我基本上面临两个问题!
1) map.setCentre() 不工作
2) 第一张地图加载正常,但第二张地图显示灰色框。
我在我的场景中尝试了几个解决方案,但似乎都没有工作!有什么帮助吗?
【问题讨论】:
标签: javascript twitter-bootstrap google-maps