Google Haritalar JavaScript API'sinin dönüşü boyunca eşzamansız yöntemler Vaatler.
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 |
Yerler Otomatik Tamamlama Hizmeti | Kısmi1 |
Street View | Evet |
Kullanım
Bunu göster kılavuz eşzamansız yöntem çağrıları yapmak için Promise'leri veya aşağıdaki örnekleri kullanma hakkında Google Maps JavaScript API'yi kullanın.
Eş zamansız yap ve bekle
await operatörü, bir Promise'i beklemek için kullanılır. Yalnızca bir asynkron işlev 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 çağırma kalıbı
Geri çağırma kalıbı hâlâ geçerli ve desteklenmektedir.
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. ↩