Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

174
Visualizações
Show "loading..." message until get the coordinate

Here I already have the script to get the coordinate (Latitude & Longitude). Since, when I clicked the button, some people wonder when the coordinate will come up. So, how to show the loading... until the coordinate is shown in the input text without reloading the page.

Here is the code:

var x = document.getElementById("lati");
var y = document.getElementById("longi");

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  } else {
    x.innerHTML = "Geolocation is not supported by this browser.";
  }
}

function showPosition(position) {
  x.innerHTML = position.coords.latitude;
  y.innerHTML = position.coords.longitude;
  document.getElementById("input_latitude").value = x.innerHTML;
  document.getElementById("input_longitude").value = y.innerHTML;
}
<div class="mb-3"><input class="border rounded-0 form-control" type="text" name="business_latitude" id="input_latitude" placeholder="Latitude" style="font-family: Roboto, sans-serif;" value="<?php echo $business_latitude; ?>" required></div>
<div class="mb-3"><input class="border rounded-0 form-control" type="text" name="business_longitude" id="input_longitude" placeholder="Longitude" style="font-family: Roboto, sans-serif;" value="<?php echo $business_longitude; ?>" required></div>
<div class="mb-3">
  <button type="button" class="btn btn-link" style="font-family: Roboto, sans-serif;" onclick="getLocation()">Current Location</button>
</div>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

var x = document.getElementById("lati");
var y = document.getElementById("longi");

function getLocation() {
  document.getElementById("input_latitude").value = "Loading...";
  document.getElementById("input_longitude").value = "Loading...";
  if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(showPosition, () => {console.log("Error")});
  } else { 
      x.innerHTML = "Geolocation is not supported by this browser.";
  }
}

function showPosition(position) {
  console.log(position);
  x.innerHTML = position.coords.latitude;
  y.innerHTML = position.coords.longitude;
  document.getElementById("input_latitude").value = x.innerHTML;
  document.getElementById("input_longitude").value = y.innerHTML;
}
<div id="lati"></div>
<div id="longi"></div>
<div class="mb-3"><input class="border rounded-0 form-control" type="text" name="business_latitude" id="input_latitude" placeholder="Latitude" style="font-family: Roboto, sans-serif;" value="<?php echo $business_latitude; ?>" required></div>
<div class="mb-3"><input class="border rounded-0 form-control" type="text" name="business_longitude" id="input_longitude" placeholder="Longitude" style="font-family: Roboto, sans-serif;" value="<?php echo $business_longitude; ?>" required></div>
<div class="mb-3">
<button type="button" class="btn btn-link" style="font-family: Roboto, sans-serif;" onclick="getLocation()">Current Location</button>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

The below code will work. I have added a <p> tag which doesn't have text in it by default. When the user clicks on the button, I paste "loading..." inside the <p> tag and when the result is shown I change it back to blank.

var x = document.getElementById("lati");
var y = document.getElementById("longi");

function getLocation() {
  document.getElementById("loadingText").innerHTML = "loading...";
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
    document.getElementById("loadingText").innerHTML = "";
  } else {
    x.innerHTML = "Geolocation is not supported by this browser.";
    document.getElementById("loadingText").innerHTML = "";
  }
}

function showPosition(position) {
  x.innerHTML = position.coords.latitude;
  y.innerHTML = position.coords.longitude;
  document.getElementById("input_latitude").value = x.innerHTML;
  document.getElementById("input_longitude").value = y.innerHTML;
}
#loadingText {
  display: block !important;
}
<div class="mb-3"><input class="border rounded-0 form-control" type="text" name="business_latitude" id="input_latitude" placeholder="Latitude" style="font-family: Roboto, sans-serif;" value="<?php echo $business_latitude; ?>" required></div>
<div class="mb-3"><input class="border rounded-0 form-control" type="text" name="business_longitude" id="input_longitude" placeholder="Longitude" style="font-family: Roboto, sans-serif;" value="<?php echo $business_longitude; ?>" required></div>
<div class="mb-3">
  <button type="button" class="btn btn-link" style="font-family: Roboto, sans-serif;" onclick="getLocation()">Current Location</button>
  <p id="loadingText"></p>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

var x = document.getElementById("lati");
var y = document.getElementById("longi");

function getLocation() {
  if (navigator.geolocation) {
     document.getElementById("input_latitude").value = "Loading...";
     document.getElementById("input_longitude").value = "Loading...";
     document.querySelectors('input').setAttribute("disabled");
     //input is disabled so that input can not be edited.
      navigator.geolocation.getCurrentPosition(showPosition, () => 
     {console.log("Error")});
  } else { 
      x.innerHTML = "Geolocation is not supported by this browser.";
  }
}

function showPosition(position) {
  console.log(position);
  x.innerHTML = position.coords.latitude;
  y.innerHTML = position.coords.longitude;
  document.getElementById("input_latitude").value = x.innerHTML;
  document.getElementById("input_longitude").value = y.innerHTML;
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda