Sözler

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

Destek

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

Kullanım

Google Maps JavaScript API ile eşzamansız yöntem çağrıları yapma hakkında bilgi edinmek için bu kılavuzu veya aşağıdaki örnekleri inceleyin.

Eş zamansız ve bekleme

await operatörü, bir Promise'in tamamlanmasını beklemek için kullanılır. Yalnızca bir async işlevi 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şlevlerini 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 çağırma kalıbı

Geri çağırma kalıbı hâlâ geçerli ve destekleniyor.

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. Sözler şu anda yalnızca getPlacePredictions()'da desteklenmektedir.