Google 地图 - 事件侦听器(`idle`、`projection_changed`)在地图滚动到视图中之前不会触发

问题描述 投票:0回答:1

我正在一个包含较长内容部分的网页上使用 Google Maps JavaScript API。该地图位于折叠下方,这意味着页面加载时最初不可见。我注意到,直到用户向下滚动并且地图容器在视口中可见之前,才会触发

idle
projection_changed
事件。

问题: 是否可以修改此行为并在地图滚动到视图之前触发事件,本质上是强制地图在页面加载时完全加载?

任何帮助将不胜感激! 🙏

在 Fiddle 中,我试图模拟一个很长的页面,顶部有一堆

h3
,然后是地图: https://jsfiddle.net/grj1smxd/2/

<!doctype html>
<!--
 @license
 Copyright 2019 Google LLC. All Rights Reserved.
 SPDX-License-Identifier: Apache-2.0
-->
<html>
  <head>
    <title>Add Map</title>
    <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
    <!-- jsFiddle will insert css and js -->
  </head>
  <body>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <h3>My Google Maps Demo</h3>
    <!--The div element for the map -->
    <div id="map"></div>

    <!-- prettier-ignore -->
    <script>(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})
        ({key: "AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg", v: "weekly"});</script>
  </body>
</html>
/**
 * @license
 * Copyright 2019 Google LLC. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */
// Initialize and add the map
let map;

async function initMap() {
  // The location of Uluru
  const position = { lat: -25.344, lng: 131.031 };
  // Request needed libraries.
  //@ts-ignore
  const { Map } = await google.maps.importLibrary("maps");
  const { AdvancedMarkerElement } = await google.maps.importLibrary("marker");

  // The map, centered at Uluru
  map = new Map(document.getElementById("map"), {
    zoom: 4,
    center: position,
    mapId: "DEMO_MAP_ID",
  });

  // The marker, positioned at Uluru
  const marker = new AdvancedMarkerElement({
    map: map,
    position: position,
    title: "Uluru",
  });
  
  map.addListener("idle", () => {
    console.log('map idel')
  });
  
  map.addListener("projection_changed", () => {
    console.log('projection cahnged');
  });
}

initMap();

我期望在页面加载时触发“projection_changed”(或“idle”)事件。

javascript google-maps events google-maps-api-3 viewport
1个回答
2
投票

我将其发布为答案,因为我还没有发表评论的权限。

你说“页面加载时,带有Google地图的部分不会显示在视口中,但必须滚动才能看到它。”,我已经检查了你的Jsfiddle并尝试删除你的一堆

h3
和地图会立即加载并显示,无需向下滚动。

您的听众

idle
projection_changed
也被触发了。

© www.soinside.com 2019 - 2024. All rights reserved.