تعرض الطرق غير المتزامنة في جميع أنحاء Google Maps JavaScript API Promises.
الدعم
| واجهة برمجة التطبيقات | تعرض الطرق الوعود |
|---|---|
| الاتجاهات | نعم |
| Distance Matrix | نعم |
| الارتفاع | نعم |
| Geocoder | نعم |
| صور التكبير إلى الحد الأقصى | نعم |
| أماكن | لا |
| Places AutocompleteService | جزئي1 |
| التجوّل الافتراضي | نعم |
الاستخدام
راجِع هذا الدليل بشأن استخدام Promises أو الأمثلة أدناه لإجراء طلبات غير متزامنة باستخدام Google Maps JavaScript API.
Async وawait
يُستخدم عامل التشغيل await لانتظار Promise. لا يمكن استخدامها إلا داخل دالة غير متزامنة.
const app = async () => {
const elevationService = google.maps.ElevationService();
const locations = [{lat: 27.986065, lng:86.922623}];
const response = await elevationService.getElevationForLocation({locations});
console.log(response.results);
};
app();
ثمّ، أمسِك بها، وأخيرًا
يحتوي الكائن Promise على الطرق then وcatch وfinally التي تتلقّى دوال رد الاتصال.
const elevationService = google.maps.ElevationService();
const locations = [{lat: 27.986065, lng:86.922623}];
const promise = elevationService.getElevationForLocation({locations});
promise
.then((response) => {
console.log(response.results);
})
.catch((error) => {
console.log(error);
});
.finally(() => {
console.log('done');
});
نمط معاودة الاتصال غير المتزامن
لا يزال نمط معاودة الاتصال صالحًا ومتاحًا.
const elevationService = google.maps.ElevationService();
const locations = [{lat: 27.986065, lng:86.922623}];
const callback = (results, status) => {
if (status === 'OK') {
console.log(results);
} else {
// handle this case
}
};
elevationService.getElevationForLocation({locations}, callback);
-
لا تتوفّر ميزة "الوعود" حاليًا إلا باللغة
getPlacePredictions(). ↩