Các phương pháp hay nhất về tương tác với GoogleMap
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Trang này giải thích các phương pháp hay nhất để tương tác với đối tượng GoogleMap
trong ứng dụng của bạn.
Triển khai các tính năng GoogleMap
trên cùng một thực thể bản đồ
Các API Maps cũng có trong Navigation SDK. Bạn có thể dùng Navigation SDK để tạo trải nghiệm dành riêng cho bản đồ (không phải điều hướng). Nếu ứng dụng của bạn có cả trải nghiệm bản đồ không có chỉ đường và trải nghiệm có chỉ đường, bạn nên sử dụng Maps và Navigation API trên cùng một thực thể bản đồ.
Sử dụng getMapAsync
để lấy thực thể GoogleMap
Bạn có thể lấy đối tượng GoogleMap
bằng cách sử dụng SupportNavigationFragment#getMapAsync
hoặc NavigationView#getMapAsync
. Chúng tôi đề xuất phương thức này thay vì giữ lại thực thể GoogleMap
có thể trở nên lỗi thời sau khi NavigationView
bên dưới được tạo lại.
Ngoài ra, hãy nhớ dọn dẹp GoogleMap
được truy xuất từ phương thức này sau khi không còn cần đến. Để biết thêm thông tin, hãy xem Các phương pháp hay nhất để dọn dẹp phiên bản.
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-31 UTC.
[null,null,["Cập nhật lần gần đây nhất: 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)."]]