Я использую navigator.geolocation.watchPosition
в JavaScript, и мне нужен способ решить, что пользователь может отправить форму, основанную на местоположении до того, как watchPosition
нашел свое местоположение.
В идеале пользователь будет периодически видеть сообщение "Ожидание местоположения", пока не будет получено местоположение, затем форма будет отправлена.
Однако я не уверен, как реализовать это в JavaScript, учитывая отсутствие функции wait
.
Текущий код:
var current_latlng = null;
function gpsSuccess(pos){
//console.log('gpsSuccess');
if (pos.coords) {
lat = pos.coords.latitude;
lng = pos.coords.longitude;
}
else {
lat = pos.latitude;
lng = pos.longitude;
}
current_latlng = new google.maps.LatLng(lat, lng);
}
watchId = navigator.geolocation.watchPosition(gpsSuccess,
gpsFail, {timeout:5000, maximumAge: 300000});
$('#route-form').submit(function(event) {
// User submits form, we need their location...
while(current_location==null) {
toastMessage('Waiting for your location...');
wait(500); // What should I use instead?
}
// Continue with location found...
});