【发布时间】:2013-02-18 08:06:19
【问题描述】:
我知道这是本网站和其他地方报告的常见问题,例如
但我无法解决我的问题。
所以我首先构建了一个响应式网站,我正在尝试让自适应地图解决方案工作,即移动视口获得静态谷歌地图图像,而对于非移动视口,完整的谷歌地图 API 和这一切都有在视口调整大小时更新自身,而不仅仅是在页面加载时。基于此:http://codepen.io/bradfrost/pen/tLxAs。
我正在使用this library。
我仅在需要时(非移动视口)通过 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>
默认情况下会加载静态地图图像,因为该网站首先是移动构建的。
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();
这是 '/includes/scripts/adaptive-google-map.php' 的内容,通过 jQuery load() 进行 AJAX 处理。
<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>
这解决了问题:
mapDynamicContainer.show(function () {
setTimeout(function() {
initialize();
}, 100);
})
但是会抛出 JS 错误:Uncaught ReferenceError: initialize is not defined。 ppl 建议的另一个常见修复方法。正在应用这个:google.maps.event.trigger(map, 'resize'); 但是当我把它放在之后:mapDynamicContainer.show(); 在match() 函数中我得到这个 JS 错误:Uncaught ReferenceError: google is not defined。
运气不好:(
【问题讨论】:
-
嘿 Chris,这里是 Nick ;-) 你有没有看到 Chris Coyier 最近在 CSS 技巧中涵盖了这个确切的场景?他甚至使用 enquire.js!你可能有兴趣看看这里:css-tricks.com/workshop-notes-webstock-13
-
另外,他在这里介绍了同样的内容,更详细一点:css-tricks.com/deseret-digital-workshop
-
感谢尼克的这些链接,但没有提到我遇到的问题,即使它们读得很好;)我确实尝试了 Chris 的技术,他使用 jQuery
.load()加载每个地图版本(img/iframe) 解决了我的问题(地图 API 未完全渲染从match->unmatch->match)但是我不能每次都在match中使用.load()加载完整的 API(非 iframe),所以每次我在match时,API 都会重新下载本身,这对性能不利 + Google 会在控制台中对此发出警告。所以我通过setup下载一次,然后[继续..] -
使用
hide/show显示相关地图(img/API),这是从match->unmatch->matchas explained here 时的问题.所有建议的修复都不起作用。 -
要修复我需要在
match中调用Google Map APIinitialize();函数,但initialize();函数在此处未定义,因为它存在于我通过@ 进行AJAX 处理的文件中987654360@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');});和 Google 地图 API 正在通过getScript异步加载。基本上我需要访问initialize();函数。
标签: jquery google-maps google-maps-api-3 responsive-design enquire.js