تعرض الطرق غير المتزامنة في جميع أنحاء Google Maps JavaScript API Promises.
الدعم
| واجهة برمجة التطبيقات | تعرض الطرق Promises |
|---|---|
| الاتجاهات | نعم |
| مصفوفة المسافات | نعم |
| الارتفاع | نعم |
| خدمة الترميز الجغرافي | نعم |
| صور التكبير إلى الحد الأقصى | نعم |
| أماكن | لا |
| 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(). ↩