แนวทางปฏิบัติแนะนำในการโต้ตอบกับ GoogleMap
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
หน้านี้อธิบายแนวทางปฏิบัติแนะนำในการโต้ตอบกับออบเจ็กต์ GoogleMap
ในแอป
ใช้ฟีเจอร์ GoogleMap
ในอินสแตนซ์แผนที่เดียวกัน
นอกจากนี้ Maps API ยังพร้อมใช้งานเป็นส่วนหนึ่งของ Navigation SDK ซึ่งคุณสามารถใช้เพื่อสร้างประสบการณ์การใช้งานแผนที่เฉพาะ (ที่ไม่ใช่การนำทาง) ได้ด้วย
หากแอปของคุณมีทั้ง
ประสบการณ์การใช้งานแผนที่ที่ไม่ใช่การนำทางและประสบการณ์การนำทาง เราขอแนะนำให้ใช้
Maps API และ Navigation API ในอินสแตนซ์แผนที่เดียวกัน
ใช้ getMapAsync
เพื่อรับอินสแตนซ์ GoogleMap
คุณรับออบเจ็กต์ GoogleMap
ได้โดยใช้ SupportNavigationFragment#getMapAsync
หรือ NavigationView#getMapAsync
เราขอแนะนำให้ใช้วิธีนี้แทนการเก็บอินสแตนซ์ GoogleMap
ที่อาจล้าสมัยเมื่อมีการสร้าง NavigationView
ที่อยู่เบื้องหลังขึ้นมาใหม่
นอกจากนี้ อย่าลืมล้างข้อมูล GoogleMap
ที่ดึงมาจากวิธีนี้หลังจากที่
ไม่จำเป็นต้องใช้อีกต่อไป ดูข้อมูลเพิ่มเติมได้ที่แนวทางปฏิบัติแนะนำในการล้างข้อมูลอินสแตนซ์
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 2025-08-31 UTC"],[[["\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)."]]