Zwrócenie wartości z funkcji |
Zwrócenie wartości z funkcji |
23.05.2020, 08:19:13
Post
#1
|
|
Grupa: Zarejestrowani Postów: 873 Pomógł: 25 Dołączył: 24.07.2005 Ostrzeżenie: (0%) |
Hej, uzywam google do wyznaczenia odleglosci.
Chcialbym z tego zwrocic po prostu wartosc dystansu i uzyc go gdzies indziej. Call do google jest asynchroniczny, probowalem przypisywac wynik do zmiennej globalnej bez skutku... |
|
|
23.05.2020, 08:41:09
Post
#2
|
|
Grupa: Zarejestrowani Postów: 6 373 Pomógł: 1116 Dołączył: 30.08.2006 Ostrzeżenie: (0%) |
Poczytaj o await i promise.
-------------------- |
|
|
23.05.2020, 09:09:14
Post
#3
|
|
Grupa: Zarejestrowani Postów: 493 Pomógł: 8 Dołączył: 7.07.2007 Skąd: Tychy Ostrzeżenie: (0%) |
Ja u mnie wykorzystuję taki kod, wywołuję tak:
Ten post edytował miccom 23.05.2020, 09:21:17 -------------------- Jeśli pomogłem- kliknij POMÓGŁ-przyda się ;)- jeśli piszę bzdury- pisz pod postami. Poprawię się.
PISZĘ POPRAWNIE PO POLSKU! |
|
|
24.05.2020, 11:10:21
Post
#4
|
|
Grupa: Zarejestrowani Postów: 873 Pomógł: 25 Dołączył: 24.07.2005 Ostrzeżenie: (0%) |
@miccom może nie podawaj swojego klucza.
@viking dzięki. napisałem coś co działa ale ... gdyby ktoś zaproponował inną wersję, inne wywołanie może, brakuje pewnie w sumie try/catch Kod function fetchDistance(origin, destination) { return new Promise(function(resolve, reject) { var service = new google.maps.DistanceMatrixService(); service.getDistanceMatrix( { origins: [origin], destinations: [destination], travelMode: google.maps.TravelMode.DRIVING, unitSystem: google.maps.UnitSystem.METRIC, avoidHighways: false, avoidTolls: false }, function(resp, status) { if (status !== google.maps.DistanceMatrixStatus.OK) { response = reject(status); } else { response = resolve(resp.rows[0].elements[0].distance.value); } }); }); } async function getDistanceInKmAsync(start, end){ const result = await fetchDistance(start, end); return result / 1000; } a używam tak Kod getDistanceAsync(ORIGIN_ADDRESS, destinationAddress).then(result => {
if(result <= <jakis_tam) { // ... } }); Ten post edytował john_doe 24.05.2020, 11:11:34 |
|
|
Wersja Lo-Fi | Aktualny czas: 22.09.2024 - 13:49 |