Google Maps JavaScript API'deki eşzamansız yöntemler Promises (Vaatler) sonucunu döndürür.
Destek
API | Promises döndürme yöntemleri |
---|---|
Yol tarifi | Evet |
Mesafe Matrisi | Evet |
Yükseltme | Evet |
Coğrafi kodlayıcı | Evet |
Maksimum Zumlu Görüntüler | Evet |
Yerler | Hayır |
Yerler Otomatik Tamamlama Hizmeti | Kısmi1 |
Street View | Evet |
Kullanım
Google Maps JavaScript API ile eşzamansız yöntem çağrıları yapmak için Vaatler'i kullanma hakkındaki bu kılavuza veya aşağıdaki örneklere bakın.
Eş zamansız yap ve bekle
bekleme operatörü, Promise'i beklemek için kullanılır. Yalnızca eşzamansız bir işlevde 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 yakalayın 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 çağırma 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. ↩