Google 地图互动最佳做法
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本页介绍了在应用中与 GoogleMap
对象互动的最佳实践。
在同一地图实例上实现 GoogleMap
功能
您还可以使用 Navigation SDK 中的 Maps API 来打造特定的地图(非导航)体验。如果您的应用同时包含非导航地图体验和导航体验,建议您在同一地图实例上使用 Maps 和 Navigation API。
使用 getMapAsync
获取 GoogleMap
实例
您可以使用 SupportNavigationFragment#getMapAsync
或 NavigationView#getMapAsync
获取 GoogleMap
对象。我们建议使用此方法,而不是保留 GoogleMap
实例,因为一旦重新创建底层 NavigationView
,该实例可能会过时。
另请务必在不再需要时清理从此方法检索到的 GoogleMap
。如需了解详情,请参阅实例清理最佳实践。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eImplement \u003ccode\u003eGoogleMap\u003c/code\u003e features on a separate map instance from navigation experiences to avoid conflicts and optimize performance.\u003c/p\u003e\n"],["\u003cp\u003eUse \u003ccode\u003egetMapAsync\u003c/code\u003e to retrieve the \u003ccode\u003eGoogleMap\u003c/code\u003e object instead of holding onto an instance, ensuring you have the most up-to-date map object and preventing stale data.\u003c/p\u003e\n"],["\u003cp\u003eRemember to clean up the \u003ccode\u003eGoogleMap\u003c/code\u003e object after use to prevent memory leaks and maintain optimal app performance, referring to the Instance cleanup best practices for detailed guidance.\u003c/p\u003e\n"]]],[],null,["# GoogleMap interaction best practices\n\nThis page explains best practices for interacting with the `GoogleMap` object in\nyour app.\n\nImplement `GoogleMap` features on the same maps instance\n--------------------------------------------------------\n\nMaps APIs are also available as part of Navigation SDK, which you can use to\nbuild map specific (non-navigation) experiences. If your app includes both a\nnon-navigation map experience and a navigation experience, we recommend using\nthe Maps and Navigation API on the same map instance.\n\nUse `getMapAsync` to obtain the `GoogleMap` instance\n----------------------------------------------------\n\nYou can obtain a `GoogleMap` object using either\n`SupportNavigationFragment#getMapAsync` or `NavigationView#getMapAsync`. We\nrecommend this method over holding onto the `GoogleMap` instance that might\nbecome stale once the `NavigationView` underneath is recreated.\n\nAlso be sure to clean up the `GoogleMap` retrieved from this method after it is\nno longer needed. For more information, see [Instance cleanup best\npractices](/maps/documentation/navigation/android-sdk/instance-cleanup-best-practices)."]]