Google Haritalar JavaScript API'sı genelinde 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 |
Rakım | Evet |
Coğrafi kodlayıcı | Evet |
Yakınlaştırılan Maksimum Görüntü Sayısı | Evet |
Yerler | Hayır |
Yerler Otomatik Tamamlama Hizmeti | Kısmi1 |
Street View | Evet |
Kullanım
Promise'leri kullanma hakkındaki bu kılavuza veya Google Maps JavaScript API ile eşzamansız yöntem çağrıları yapmak için aşağıdaki örneklere bakın.
Eş zamansız ve beklemede
await operatörü, bir Promise'i beklemek için kullanılır. Yalnızca eşzamansız bir 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();
Sonra yakalama ve son olarak
Promise nesnesinde, geri çağırma işlevleri alan then
, catch
ve finally
yöntemleri bulunur.
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 kalıbı
Geri çağırma kalıbı hâlâ geçerlidir 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);
-
Vaatler şu anda yalnızca
getPlacePredictions()
dilinde desteklenmektedir. ↩