开始使用
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在本教程中,您将指导您使用 Maps JavaScript 中的仿真 3D 地图创建您的第一个 JavaScript 应用:一个基本窗口,用于显示金门大桥的俯视图,背景是马林岬。
完成本教程后,您应该会在开发环境中看到以下地图:
设置环境
在开始编写代码之前,您必须设置一个运行 JavaScript 的环境。在本教程中,您将使用网络浏览器作为环境。所有新型网络浏览器都内置了对 JavaScript 的支持,因此您无需安装任何其他软件。
- 打开您选择的文本编辑器。
- 创建一个新文件并以
.html
扩展名(例如hello-p3djs.html
)。
编写 HTML 网页
首先,您需要创建一个具有基本 HTML 结构的网页:
<!DOCTYPE html>
<html>
<head>
<title>Hello Photorealistic 3D Maps in Maps JavaScript</title>
</head>
<body>
<!-- Your JavaScript code will go here -->
</body>
</html>
添加 JavaScript
接下来,您需要添加自定义 HTML 元素来加载地图。该代码包含两个元素:
gmp-map-3d
包含用于初始化镜头起始位置和视图的参数。
script
包含用于加载 Maps JavaScript API 的调用。请务必将 YOUR_KEY
替换为您的 API 密钥。
<!DOCTYPE html>
<html>
<head>
<title>Hello Photorealistic 3D Maps in Maps JavaScript</title>
<style>
html,
body {
height:100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<gmp-map-3d mode="hybrid" center="37.841157, -122.551679" range="2000" tilt="75" heading="330"></gmp-map-3d>
<script async src="https://maps.googleapis.com/maps/api/js?key=<YOUR_KEY>&v=beta&libraries=maps3d"></script>
</body>
</html>
运行应用
如需运行该应用并查看输出,请按以下步骤操作:
- 保存您创建的 HTML 文件。
- 在网络浏览器中打开文件(您可以双击文件,将其拖到浏览器窗口中,或右键点击文件并使用“打开方式”)。
- 您应该会在浏览器窗口中看到地图。
恭喜!您刚刚使用 Maps JavaScript API 中的 Google 仿真 3D 地图编写了一个应用。
后续步骤
- 利用 Google 的现有示例打造更复杂的 3D 地图体验。
- 阅读参考文档,探索 Maps JavaScript API 中仿真 3D 地图的全部潜力。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-04-29。
[null,null,["最后更新时间 (UTC):2025-04-29。"],[],[],null,["\u003cbr /\u003e\n\n| This product or feature is in Preview (pre-GA). Pre-GA products and features might have limited support, and changes to pre-GA products and features might not be compatible with other pre-GA versions. Pre-GA Offerings are covered by the [Google\n| Maps Platform Service Specific Terms](https://cloud.google.com/maps-platform/terms/maps-service-terms). For more information, see the [launch stage\n| descriptions](/maps/launch-stages).\n\n\u003cbr /\u003e\n\nSelect platform: [Android](/maps/documentation/maps-3d/android-sdk/add-a-3d-map \"View this page for the Android platform docs.\") [iOS](/maps/documentation/maps-3d/ios-sdk/add-a-3d-map \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/javascript/3d/get-started \"View this page for the JavaScript platform docs.\")\n\n\u003cbr /\u003e\n\nIn this tutorial, you'll guide yourself through creating your first JavaScript\napplication using Photorealistic 3D Maps in Maps JavaScript: a basic window that displays\nan overhead view of the Golden Gate Bridge with the Marin Headlands in the\nbackground.\n\nUpon completing the tutorial, you should see the following map in your\ndevelopment environment:\n\nSet up your environment\n\nBefore you begin writing code, you must set up an environment that runs\nJavaScript. For this tutorial, you'll use a web browser as your environment. All\nmodern web browsers have built-in support for JavaScript, so you don't need to\ninstall any additional software.\n\n1. Open a text editor of your choosing.\n2. Create a new file and save it with an `.html` extension (e.g., `hello-p3djs.html`).\n\nWrite an HTML page\n\nTo start, you'll create a web page with a basic HTML structure: \n\n \u003c!DOCTYPE html\u003e\n \u003chtml\u003e\n \u003chead\u003e\n \u003ctitle\u003eHello Photorealistic 3D Maps in Maps JavaScript\u003c/title\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n \u003c!-- Your JavaScript code will go here --\u003e\n \u003c/body\u003e\n \u003c/html\u003e\n\nAdd JavaScript\n\nNext, you'll add a custom HTML element to load the map. The code contains two\nelements:\n\n- `gmp-map-3d` contains the parameters used for initializing the starting camera position and view.\n- `script` contains the call to load the Maps JavaScript API. Be sure to replace `YOUR_KEY` with your API key.\n\n \u003c!DOCTYPE html\u003e\n \u003chtml\u003e\n \u003chead\u003e\n \u003ctitle\u003eHello Photorealistic 3D Maps in Maps JavaScript\u003c/title\u003e\n\n \u003cstyle\u003e\n html,\n body {\n height:100%;\n margin: 0;\n padding: 0;\n }\n \u003c/style\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n \u003cgmp-map-3d mode=\"hybrid\" center=\"37.841157, -122.551679\" range=\"2000\" tilt=\"75\" heading=\"330\"\u003e\u003c/gmp-map-3d\u003e\n \u003cscript async src=\"https://maps.googleapis.com/maps/api/js?key=\u003cYOUR_KEY\u003e&v=beta&libraries=maps3d\"\u003e\u003c/script\u003e\n \u003c/body\u003e\n \u003c/html\u003e\n\nRun the application\n\nTo run the application and see the output, follow these steps:\n\n1. Save the HTML file you created.\n2. Open the file in a web browser (you can double-click the file, drag it into a browser window, or right-click and use \"Open with\").\n3. You should see the map in your browser window.\n\nCongratulations! You've just written an application using Google's\nPhotorealistic 3D Maps in Maps JavaScript API.\n\nNext steps\n\n- Build more complicated 3D map experiences using Google's existing [samples](/maps/documentation/javascript/examples/3d/simple-map).\n- Discover the full potential of the Photorealistic 3D Maps in Maps JavaScript API by reading the [reference documentation](/maps/documentation/javascript/reference/3d-map)."]]