You need to enable JavaScript to run this app. Google Maps JavaScript API在React应用中无法显示问题求助
优惠活动
大模型
产品
解决方案
定价
更多

Google Maps JavaScript API在React应用中无法显示问题求助

问题核心原因

  1. 你在React组件的render函数中直接插入<script>标签,React不会执行这类动态插入的脚本,Google Maps API的加载逻辑无法正常触发
  2. React中DOM元素的类名属性需要使用className而非class,你原有代码的class属性全部不生效,可能导致样式异常
  3. 你定义的initMap函数属于模块内部作用域,没有挂载到全局window对象上,Google Maps API加载完成后调用全局initMap时找不到对应函数
  4. 地图初始化执行时机错误,可能出现API调用initMap时,id为map的DOM元素还未挂载到真实DOM树,导致document.getElementById("map")返回空值
  5. React组件内不应该返回<body>标签,React项目仅在入口public/index.html中存在唯一的根body节点,重复定义会导致DOM结构异常

修复方案

  1. 将Google Maps API的引入代码移到public/index.html的<body>标签末尾,不要放在组件的render逻辑中:
<script src="https://maps.googleapis.com/maps/api/js?key=替换为你的实际API_KEY&callback=initMap&v=weekly" async></script>
  1. 原有代码中所有DOM元素的class属性全部替换为className,保证样式正常生效
  2. 在组件的挂载生命周期中绑定全局initMap函数,确保DOM加载完成后再初始化地图:

类组件写法

class YourComponent extends React.Component {
  // 组件挂载完成后执行
  componentDidMount() {
    // 将initMap挂载到全局window对象,供Google Maps API调用
    window.initMap = () => {
      this.map = new window.google.maps.Map(document.getElementById("map"), {
        center: { lat: -34.397, lng: 150.644 },
        zoom: 8,
      });
    }
  }

  // 组件卸载前清理资源,避免内存泄漏
  componentWillUnmount() {
    delete window.initMap
    this.map = null
  }

  render() {
    // 不要返回body标签,用空标签包裹内容即可
    return (
      <>
        <nav>    
          <ul className="nav-links">
            <li><a href="#"> <img className="home_img" id="1" src={home} ></img> <div id="texto1"> <p>Home</p></div> </a></li>
            <li><a href="#"> <img className="home_img" id="2" src={alertas}></img>  <div id="texto2"><p>Alertas</p></div> </a></li>
            <li><a href="#"> <img className="home_img" id="3" src={dashboards} ></img>  <div id="texto3"><p>Dashboards</p></div> </a></li>
            <li><a href="#"> <img className="home_img" id="4" src={locais} ></img> <div id="texto4"> <p>Os seus locais</p></div> </a></li>
            <li><a href="#"> <img className="home_img" id="5" src={ajuda} ></img> <div id="texto5"> <p>Ajuda</p></div> </a></li>
          </ul>
          <div className="burger" onClick={()=>this.abreBarra()}>
            <div className="line1"></div>
            <div className="line2"></div>
            <div className="line3"></div>
          </div>
          <div className="logo">
            <h4>Crowdzero</h4>
          </div>
        </nav>
        <div id="map"></div>
      </>
    );
  }
}

函数组件写法(适配React 16.8+版本)

import { useEffect } from 'react'

const YourComponent = () => {
  useEffect(() => {
    window.initMap = () => {
      new window.google.maps.Map(document.getElementById("map"), {
        center: { lat: -34.397, lng: 150.644 },
        zoom: 8,
      });
    }
    // 组件卸载时清理全局资源
    return () => {
      delete window.initMap
    }
  }, [])

  // 渲染逻辑和类组件保持一致
  return (
    <>
      {/* 导航、地图容器等代码 */}
    </>
  )
}

内容的提问来源于stack exchange,提问作者Enguias

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 09:15:08