我的页面已加载谷歌地图但使用Jquery我隐藏它。点击按钮,它应显示我在该页面中加载的谷歌地图。但我无法看到完整的地图,即。我只看到地图的一部分。 以下是我的代码: -
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style id="my-css">
map,div[map] {
display: block;
width: 600px;
height: 400px
}
</style>
<script src="https://maps.google.com/maps/api/js?sensor=false"></script>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
<script src="scripts/plunkr.js"></script>
<script src="scripts/app.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps?file=api&v=2&sensor=true&key=ABQIAAAAa24xicak8_ghHX58i7La7hRFh9iM79SNC94rOejOtdMRvQmJiBS6Uv5F_1BNSh9ZuSzFXyekHISgew"> </script>
<script
src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
function initialize()
{
var mapProp = {
center:new google.maps.LatLng(51.508742,-0.120850),
zoom:7,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map=new google.maps.Map(document.getElementById("map-canvas"),mapProp);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<script>
$(document).ready(function(){
$("#maps12").hide();
$("button").click(function(){
$("#maps12").show();
});
});
</script>
<script src="../dist/ng-map.min.js"></script>
</head>
<body>
<div class="main">
<header>
DEMO
</header>
<section>
<div id="maps12">
<div id="map-canvas" style="width: 600px; height: 400px"></div>
</div>
</section>
<footer>
<button>click me</button>
</footer>
</div>
</body>
</html>
答案 0 :(得分:1)
您正在使用窗口的onload
事件和隐藏元素加载Google地图。然后你再次显示它所以地图有点卡住以调整大小。
因此,当您显示div时,Google地图的trigger the resize
事件就像
$(document).ready(function() {
$("#maps12").hide();
$("button").click(function() {
$("#maps12").show();
google.maps.event.trigger(map, "resize");
});
});