Thông báo: Kiểu bản đồ cơ sở mới sắp ra mắt trên Nền tảng Google Maps. Bản cập nhật này về kiểu bản đồ bao gồm bảng màu mặc định mới, ghim hiện đại và các điểm cải tiến về trải nghiệm và khả năng sử dụng bản đồ. Tất cả kiểu bản đồ sẽ được tự động cập nhật vào tháng 3 năm 2025. Để biết thêm thông tin về phạm vi cung cấp và cách chọn sử dụng sớm hơn, hãy xem bài viết Kiểu bản đồ mới cho Nền tảng Google Maps.
Hãy xem hướng dẫn này về cách sử dụng Lời hứa hoặc các ví dụ bên dưới để thực hiện lệnh gọi phương thức không đồng bộ bằng API JavaScript của Google Maps.
Không đồng bộ và chờ
Toán tử await được dùng để chờ một Lời hứa. Bạn chỉ có thể sử dụng hàm này bên trong một hàm không đồng bộ.
constelevationService=google.maps.ElevationService();constlocations=[{lat:27.986065,lng:86.922623}];constcallback=(results,status)=>{if(status==='OK'){console.log(results);}else{// handle this case}};elevationService.getElevationForLocation({locations},callback);
Hiện tại, Promises chỉ được hỗ trợ trong getPlacePredictions(). ↩
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-01-15 UTC."],[[["Asynchronous methods within the Google Maps JavaScript API predominantly return Promises for efficient handling of operations."],["Numerous Google Maps services, including Directions, Distance Matrix, Elevation, Geocoder, and Streetview, utilize Promises in their methods."],["Developers can employ async/await, then/catch/finally, or traditional callbacks to manage asynchronous operations and responses effectively."],["While Places generally do not utilize Promises, the `getPlacePredictions()` method within the Places AutocompleteService does offer partial Promise support."],["Beginning in 2020, all newly introduced APIs within the Google Maps JavaScript API exclusively support Promises for asynchronous operations."]]],[]]