ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

Next.js 中使用 @next/third-parties 集成 Google Maps Embed:完整示例与实现原理

Next.js 中使用 @next/third-parties 集成 Google Maps Embed:完整示例与实现原理 Next.js 中使用 next/third-parties 集成 Google Maps Embed完整示例与实现原理【免费下载链接】next.jsThe React Framework项目地址: https://gitcode.com/GitHub_Trending/next/next.js本文围绕 Next.js 官方仓库中的examples/with-google-maps-embed示例展开讲解如何利用next/third-parties/google包提供的GoogleMapsEmbed组件在 App Router 应用中快速嵌入 Google 地图。读完本文你将掌握该示例的完整启动步骤、环境变量配置方法、组件全部可用属性与五种渲染模式并能透过 google-maps-embed.tsx 源码理解其由第三方生成 iframe HTML 再注入页面的实现机制。示例概览一个组件搞定地图嵌入examples/with-google-maps-embed是一个最小化的 Next.jsApp Router TypeScript应用其核心代码仅位于 app/page.tsx 中通过next/third-parties的GoogleMapsEmbed组件渲染一张指向布鲁克林大桥的嵌入式地图。import { GoogleMapsEmbed } from next/third-parties/google; const API_KEY process.env.NEXT_PUBLIC_GOOGLE_API_KEY; export default function Page() { return ( div GoogleMapsEmbed apiKey{API_KEY} height{400} width{700} modeplace qBrooklynBridge,NewYork,NY / /div ); }app/layout.tsx 仅定义了标准根布局并输出页面标题与描述元信息示例的 package.json 声明了dev/build/start三个标准脚本依赖为next/third-parties、next、react与react-domReact 19。它展示的正是 Next.js 官方推荐的通过组件加载第三方库的接入形态。快速启动从脚手架到本地运行1. 用 create-next-app 拉取示例示例 README 提供了使用 npm、Yarn 与 pnpm 三种脚手架方式任选其一即可npx create-next-app --example with-google-maps-embed with-google-maps-embed-appyarn create next-app --example with-google-maps-embed with-google-maps-embed-apppnpm create next-app --example with-google-maps-embed with-google-maps-embed-app2. 配置 API Key 环境变量Google Maps Embed 的请求必须在浏览器端携带 API Key因此示例将该 Key 放在.env.local文件中。示例说明中的做法是复制模板文件cp .env.local.example .env.local若你的本地克隆中未包含该模板文件也可以直接手工创建.env.local并写入同一变量名。随后将.env.local中的NEXT_PUBLIC_GOOGLE_API_KEY设置为你的 Google Maps API Key# .env.local NEXT_PUBLIC_GOOGLE_API_KEY你的_API_Key变量必须以NEXT_PUBLIC_为前缀在 Next.js 中只有带此前缀的变量才会在构建期被内联进浏览器端打包产物而 app/page.tsx 正是直接process.env.NEXT_PUBLIC_GOOGLE_API_KEY取值并传入组件因此命名前缀不可省略。.env.local会被 Git 忽略不会提交到仓库。3. 启动验证npm run dev # 或 yarn dev / pnpm dev开发模式访问 http://localhost:3000 npm run build # 生产构建 npm run start # 运行生产版本获取 API Key 并在.env.local中配置正确后页面即可显示嵌入式地图该示例也支持直接部署到支持 Next.js 的云平台。组件属性全解析五种 mode 与完整参数表GoogleMapsEmbed的 TypeScript 类型定义位于 packages/third-parties/src/types/google.ts其中完整声明了组件支持的全部属性属性类型必填说明apiKeystring是Google Maps Embed API Keymodeplace \| view \| directions \| streetview \| search是地图嵌入模式qstring否查询关键词或地点多个词用连接centerstring否地图中心坐标如40.7128,-74.0060与zoom配合使用zoomstring否缩放级别maptypestring否地图类型如roadmap、satellitelanguagestring否界面语言regionstring否地区代码影响本地化与默认倾向heightnumber \| string否容器高度默认autowidthnumber \| string否容器宽度默认autoloadingeager \| lazy否加载策略默认懒加载见下节allowfullscreenboolean否是否允许全屏stylestring否注入到嵌入元素上的内联样式idstring否元素 idmode决定嵌入哪种地图视图示例中使用的是place地点。五种模式各自的应用场景为place显示某个具体地点/商户通过q指定地点如示例中的qBrooklynBridge,NewYork,NYview静态地图视角用centerzoom定位适合展示一块区域directions路线规划视图streetview街景视图search一般性地图搜索q支持任意搜索词。由于组件只做渲染 iframe/HTML工作而不调用地图 JS SDK因此即便在无交互的页面中它也保持轻量。从源码看实现原理组件只是对 third-party-capital 的薄封装packages/third-parties/src/google/google-maps-embed.tsx 的实现非常精简——它把参数透传给第三方包third-party-capital声明于 packages/third-parties/package.json 的 dependencies版本为1.0.20由其生成完整的嵌入 HTMLimport { GoogleMapsEmbed as TPCGoogleMapEmbed } from third-party-capital import ThirdPartyScriptEmbed from ../ThirdPartyScriptEmbed export default function GoogleMapsEmbed(props: GoogleMapsEmbedTypes) { const { apiKey, ...restProps } props const formattedProps { ...restProps, key: apiKey } const { html } TPCGoogleMapEmbed(formattedProps) return ( ThirdPartyScriptEmbed height{formattedProps.height || null} width{formattedProps.width || null} html{html} dataNtpcGoogleMapsEmbed /ThirdPartyScriptEmbed ) }关键点apiKey被单独解构出来以key的名义重新并入formattedProps传递给third-party-capital。该包按 Google Maps Embed 官方规则拼装带key参数的iframeHTML 字符串本仓库并不直接处理地图数据或鉴权Key 校验发生在 Google 服务端。入口 packages/third-parties/src/google/index.tsx 将该组件与YouTubeEmbed、GoogleTagManager、GoogleAnalytics一并从next/third-parties/google导出。注入 HTML 与特性埋点html最终交由 packages/third-parties/src/ThirdPartyScriptEmbed.tsx 渲染。该文件顶部标注use client说明这是一个客户端组件因此即使示例的 app/page.tsx 本身是 Server Component也能正确完成 DOM 注入与副作用执行。其核心逻辑有两处HTML 注入将第三方生成的 HTML 通过dangerouslySetInnerHTML注入到一个 div 中并按传入的height/width数值时自动追加px未传时为auto设置容器尺寸外层同时标记data-ntpcGoogleMapsEmbed便于识别特性使用埋点在useEffect中调用performance.mark(mark_feature_usage, { detail: { feature: next-third-parties-GoogleMapsEmbed } })。源码注释说明这是作为特性使用信号而非性能基准开销极低且 API 广泛可用。此外packages/third-parties/README.md 明确指出GoogleMapsEmbed默认利用loading属性对首屏the fold以下的地图进行懒加载避免嵌入 iframe 拖慢页面初始加载——这也是该组件推荐用于页脚地图、联系方式区块等非首屏位置的原因。若地图位于首屏且必须立即显示可显式传loadingeager。文件导航与延伸阅读示例主体examples/with-google-maps-embed/app/page.tsx、examples/with-google-maps-embed/app/layout.tsx示例配置examples/with-google-maps-embed/package.json、examples/with-google-maps-embed/tsconfig.json组件类型声明packages/third-parties/src/types/google.ts组件实现packages/third-parties/src/google/google-maps-embed.tsxHTML 注入容器packages/third-parties/src/ThirdPartyScriptEmbed.tsx包文档与导出入口packages/third-parties/README.md、packages/third-parties/src/google/index.tsxnext/third-parties在 packages/third-parties/package.json 中被声明为处于实验性、持续开发中的包peer 依赖next^13 及以上。实际接入生产项目前建议对照该包文档与 TypeScript 类型 确认 API 稳定性并结合 examples/with-google-maps-embed/README.md 的部署说明完成上线。综上with-google-maps-embed示例展示了接入第三方内容的最短路径一行导入、一个组件、一份NEXT_PUBLIC_环境变量即可让页面具备完整的 Google 地图能力而将脚本/iframe 注入懒加载等细节收敛到next/third-parties内部正是其在优化第三方库加载上提供的核心价值。【免费下载链接】next.jsThe React Framework项目地址: https://gitcode.com/GitHub_Trending/next/next.js创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表