如何检查Google地图是否已满载?

本文探讨了在网站中嵌入Google地图时,如何检测地图及其所有平铺是否完全加载的方法。针对不同版本的Google Maps API,提供了多种监听事件和方法,如tilesloaded(), bounds_changed等,确保在地图加载完成后启动JavaScript流程。

本文翻译自:How can I check whether Google Maps is fully loaded?

I'm embedding Google Maps into my web site. 我正在将Google地图嵌入我的网站。 Once Google Maps is loaded, I need to kick off a few JavaScript processes. 加载Google地图后,我需要启动一些JavaScript流程。

Is there a way to auto-detect when Google Maps has fully loaded, including tile downloads and all? 有没有办法在Google地图完全加载时自动检测,包括平铺下载等等?

A tilesloaded() method exists that is supposed to accomplish exactly this task but it does not work . 存在一个tilesloaded()方法,该方法应该完成此任务,但它不起作用


#1楼

参考:https://stackoom.com/question/3uCw/如何检查Google地图是否已满载


#2楼

If you're using the Maps API v3, this has changed. 如果您使用的是Maps API v3,则会发生变化。

In version 3, you essentially want to set up a listener for the bounds_changed event, which will trigger upon map load. 在版本3中,您基本上想要为bounds_changed事件设置一个侦听器,该事件将在映射加载时触发。 Once that has triggered, remove the listener as you don't want to be informed every time the viewport bounds change. 触发后,删除侦听器,因为每次视口边界更改时都不希望被通知。

This may change in the future as the V3 API is evolving :-) 随着V3 API的发展,这可能会在未来发生变化:-)


#3楼

I'm creating html5 mobile apps and I noticed that the idle , bounds_changed and tilesloaded events fire when the map object is created and rendered (even if it is not visible). 我正在创建html5移动应用程序,我注意到在创建和呈现地图对象时(即使它不可见),会tilesloaded idlebounds_changedtilesloaded事件。

To make my map run code when it is shown for the first time I did the following: 为了使我的地图在第一次显示时运行代码,我执行了以下操作:

google.maps.event.addListenerOnce(map, 'tilesloaded', function(){
    //this part runs when the mapobject is created and rendered
    google.maps.event.addListenerOnce(map, 'tilesloaded', function(){
        //this part runs when the mapobject shown for the first time
    });
});

#4楼

You could check the GMap2.isLoaded() method every n milliseconds to see if the map and all its tiles were loaded ( window.setTimeout() or window.setInterval() are your friends). 您可以每隔n毫秒检查GMap2.isLoaded()方法,以查看是否已加载地图及其所有切片( window.setTimeout()window.setInterval()是您的朋友)。

While this won't give you the exact event of the load completion, it should be good enough to trigger your Javascript. 虽然这不会为您提供加载完成的确切事件,但它应该足以触发您的Javascript。


#5楼

Where the variable map is an object of type GMap2: 变量map是GMap2类型的对象:

    GEvent.addListener(map, "tilesloaded", function() {
      console.log("Map is fully loaded");
    });

#6楼

If you're using web components , then they have this as an example: 如果您正在使用Web组件 ,那么他们将此作为示例:

map.addEventListener('google-map-ready', function(e) {
   alert('Map loaded!');
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值