تعرض الطرق غير المتزامنة في Google Maps JavaScript API التعهدات.
الدعم
API | إرجاع الطرق |
---|---|
الاتجاهات | نعم |
مصفوفة المسافة | نعم |
الارتفاع | نعم |
برنامج ترميز المواقع الجغرافية | نعم |
الحد الأقصى لعدد الصور التي يمكن التكبير/التصغير | نعم |
الأماكن | لا |
Places AutocompleteService | جزئي1 |
التجوّل الافتراضي | نعم |
الاستخدام
اطّلع على هذا الدليل حول استخدام "الوعود" أو الأمثلة أدناه لإجراء استدعاءات طرق غير متزامنة باستخدام واجهة برمجة تطبيقات JavaScript لخرائط Google.
غير متزامن والانتظار
يتم استخدام العامل المنتظَر لانتظار وعد. ولا يمكن استخدامها إلا داخل دالة غير متزامنة.
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()
.↩