Sözler

Google Maps JavaScript API'deki eşzamansız yöntemler Promise döndürür.

Destek

API Yöntemler Promise döndürür
Yol tarifi Evet
Mesafe Matrisi Evet
Yükseklik Evet
Coğrafi kodlayıcı Evet
Maksimum Yakınlaştırma Görüntüleri Evet
Yerler Hayır
Places AutocompleteService Kısmi1
Streetview Evet

Kullanım

Promise'leri kullanmayla ilgili bu kılavuza veya Google Maps JavaScript API ile asenkron yöntem çağrıları yapmak için aşağıdaki örneklere bakın.

Eş zamansız ve bekleme

await operatörü, bir Promise'i beklemek için kullanılır. Yalnızca bir asynkron işlevin içinde kullanılabilir.

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

Ardından, yakalama ve son olarak

Promise nesnesi, geri çağırma işlevleri alan then, catch ve finally yöntemlerine sahiptir.

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

Eş zamansız geri arama deseni

Geri çağırma kalıbı geçerliliğini korur ve desteklenir.

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. Şu anda Promise'ler yalnızca getPlacePredictions()'te desteklenmektedir.