Chr*_*rce 2 jquery google-maps google-maps-api-3 responsive-design enquire.js
我知道这是本网站及其他地方报道的常见问题
但我不能解决我的问题.
所以我首先建立了一个响应式网站构建Mobile,我正在尝试使用自适应地图解决方案,即移动视口获取静态Google地图图像,非移动视口使用完整的Google地图API,这一切都必须自行更新当视口不仅仅在页面加载时调整大小.基于此:http://codepen.io/bradfrost/pen/tLxAs.
我正在使用这个库.
我只是在需要时(非移动视口)通过jQuery $.getScript和库setup()和defer()函数有条件地加载Maps API .
因此,在每个视口中只有正确的地图渲染我正在使用jQuery hide(),show()因此API地图不会完全加载自身(只有1个图块),如果页面加载到非移动视口,它确实可以正常加载大小,当您将移动视口调整为非移动视口大小时,就像show()方法启动时一样.其他一切正常.
这是我的代码:
HTML
<div class="map">
<div class="map__static">
<img src="http://maps.google.com/maps/api/staticmap?center=-33.867487,151.20699&zoom=15&markers=-33.867487,151.20699&size=650x405&sensor=false">
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
默认情况下会加载静态地图图像,因为网站首先构建为Mobile.
JS
var mapContainer = $('.map'),
mapStaticContainer = $('.map__static');
mapDynamicContainer = $('<div class="map__dynamic"/>');
// Start Enquire library
enquire.register('screen and (min-width: 40em)', {
deferSetup: true,
setup: function setup() {
mapContainer.prepend(mapDynamicContainer);
mapDynamicContainer.load('/includes/scripts/adaptive-google-map.php', function() {
$.getScript('https://maps.googleapis.com/maps/api/js?key=AIzaSyAywjPmf5WvWh_cIn35NwtIk-gYuwu1I2Q&sensor=false&callback=initialize');
});
},
// Not mobile size viewport
match: function() {
mapStaticContainer.hide();
mapDynamicContainer.show();
},
// Mobile size viewport
unmatch: function() {
mapStaticContainer.show();
mapDynamicContainer.hide();
}
}, true).listen();
Run Code Online (Sandbox Code Playgroud)
这是'/includes/scripts/adaptive-google-map.php'的内容,它是通过jQuery进行的AJAX load().
<div id="map_canvas"></div>
<script>
function initialize() {
var myLatlng = new google.maps.LatLng(-33.867487,151.20699);
var myOptions = {
zoom: 15,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: false
}
map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
animation: google.maps.Animation.DROP
});
var contentString =
'<div class="infowindow">'+
'<div>'+
'<p class="flush"><strong>SALES OFFICE:</strong><br>1/16 M. 5<br>Sydney<br>NSW, 83110.</p>'
'</div>'+
'</div>'
;
var infowindow = new google.maps.InfoWindow({
content: contentString
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
// Center Google Maps (V3) on browser resize (http://stackoverflow.com/questions/8792676/center-google-maps-v3-on-browser-resize-responsive)
var center;
function calculateCenter() {
center = map.getCenter();
}
google.maps.event.addDomListener(map, 'idle', function() {
calculateCenter();
});
google.maps.event.addDomListener(window, 'resize', function() {
map.setCenter(center);
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
这解决了这个问题:
mapDynamicContainer.show(function () {
setTimeout(function() {
initialize();
}, 100);
})
Run Code Online (Sandbox Code Playgroud)
但抛出JS错误:Uncaught ReferenceError: initialize is not defined.ppl建议的另一个常见修复.应用这个:google.maps.event.trigger(map, 'resize');但是当我把它放在:mapDynamicContainer.show();在match()函数中我得到这个JS错误:Uncaught ReferenceError: google is not defined.
没有太多运气:(
好的,所以基本上"调整大小"仍然是你的答案.
我刚刚将你的匹配修改为这段代码:
match: function() {
mapStaticContainer.hide();
mapDynamicContainer.show();
if( typeof(map) != 'undefined'){
var center = map.getCenter();
google.maps.event.trigger(map, "resize");
map.setCenter(center);
}
},
Run Code Online (Sandbox Code Playgroud)
它正在发挥作用.不知道你的代码中是否还有其他东西.我使用了html,js和你提供的adaptive-google-map.php的内容,它没有任何问题.