Chrome 谷歌MapAPI灰色屏幕

zbwhf8kr  于 2023-08-01  发布在  Go
关注(0)|答案(1)|浏览(118)

我已经从Google的文档中复制并粘贴了代码,并插入了我自己的API密钥。加载时,Map会 Flink 一小秒钟,然后由于某种原因消失。曾试图寻找这样的洞察力,但无济于事。

<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: "REDACTED",
    v: "weekly",
  });

  async function initMap() {
    const
      { Map } = await google.maps.importLibrary('maps')
    map = new Map(document.querySelector('#main > div:first-child > section:nth-child(1)'), {
      center: { lat: -34.397, lng: 150.644 },
      zoom: 8,
    })
    // google.maps.event.trigger(map, 'resize');
  }

  initMap()
</script>

字符串


的数据


xjreopfe

xjreopfe1#

您的代码运行良好,因为您使用了正确的HTML元素(在您发布的代码中找不到),并且您已经在CSS中声明了map容器的高度(在您发布的代码中找不到)。

(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: "AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk",
    v: "weekly",
  });

async function initMap() {
  const {
    Map
  } = await google.maps.importLibrary('maps')
  map = new Map(document.querySelector('#map'), {
    center: {
      lat: -34.397,
      lng: 150.644
    },
    zoom: 8,
  })
}

initMap()
#map {
  height: 180px;
}
<div id="map">

</div>

相关问题