وعده ها

روش‌های ناهمزمان در سراسر Google Maps JavaScript API Promises را برمی‌گرداند.

حمایت کردن

API روش های بازگشت وعده ها
جهت ها آره
ماتریس فاصله آره
ارتفاع آره
ژئوکدر آره
حداکثر زوم تصاویر آره
مکان ها خیر
AutocompleteService را مکان می دهد جزئی 1
نمای خیابان آره

استفاده

این راهنمای استفاده از Promises یا مثال‌های زیر را برای برقراری تماس‌های روش ناهمزمان با Google Maps JavaScript API ببینید.

همگام سازی کنید و منتظر بمانید

عملگر await برای انتظار یک Promise استفاده می شود. فقط می تواند در داخل یک تابع async استفاده شود.

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);

  1. در حال حاضر Promises فقط در getPlacePredictions() پشتیبانی می شود.