简体   繁体   English

将响应式Google地图居中

[英]Centering Responsive Google Maps

As you can see from this jsFiddle , I have a responsive google map thanks to this stack overflow question . 从此jsFiddle中可以看到,由于此堆栈溢出问题 ,我得到了一个响应式Google地图。

Now the problem is to have the map remain centered on the marker on different viewports and browser resizes. 现在的问题是使地图保持在不同视口上标记的中心并调整浏览器的大小。

I have found an answer here with the following code: 在这里找到了以下代码的答案

google.maps.event.addDomListener(window, 'resize', function() {
map.setCenter(homeLatlng);
});

However, since I am still not very good with jQuery and javascript, I have no idea how to apply the event listener. 但是,由于我对jQuery和javascript仍然不太满意,因此我不知道如何应用事件监听器。

Does anyone know how the event listener would be applied? 有谁知道事件监听器将如何应用?

I don't think your second code snippet will work in conjunction with the first as you're embedding your map using an iFrame. 当您使用iFrame嵌入地图时,我认为您的第二个代码段不能与第一个代码段一起使用。 This means you have no access to the map to manipulate it via its API (which is what the second code snippet is attempting to do). 这意味着您无权访问地图以通过其API进行操作(这是第二个代码段正在尝试执行的操作)。

I'd suggest you take a look at the Google maps API 3 getting started guide which will give you all the information you need to get going. 建议您阅读Google Maps API 3入门指南 ,该指南将为您提供入门所需的所有信息。 You should be able to combine the 'hello world' example with your responsive CSS and the event handler above to make the map do exactly what you want. 您应该能够将“ hello world”示例与您的响应式CSS和上面的事件处理程序结合使用,以使地图完全按照您想要的方式工作。

It's a really intuitive API and the documentation is fantastic, so I don't think you'll have any trouble. 这是一个非常直观的API,文档非常棒,因此我认为您不会遇到任何麻烦。 If you do run into difficulties you know exactly where to come! 如果您确实遇到困难,那么您将确切知道该怎么办!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM