Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

194
Vistas
Convert Sting to [x, y] coordinates in javascript

<script type="text/javascript">
  var src = [40.6976, -74.2598];

  function convertToDMS(src) {
    function toDMS(n) {
      n = Math.abs(n);
      var d = Math.floor(n);
      n = n - d;
      n *= 60;
      var m = Math.floor(n);
      n = n - m;
      n *= 60;
      var s = Math.floor(n);
      return "" + d + " " + m + " " + s;
    }

    var dir0 = src[0] > 0 ? "N" : "S";
    var dir1 = src[1] > 0 ? "E" : "W";

    console.log(toDMS(src[0]) + dir0);
    console.log(toDMS(src[1]) + dir1);
  }

  convertToDMS(src);
</script>
<div id="locationdiv">[40.6976,-74.2598]</div>

Using the script above I am able to get degree, minute and second like 40°41'46.0"N+74°18'08.0"W

However if instead of using 
var src = [40.6976,-74.2598];
I use
var src = document.getElementById("locationdiv").innerHTML;

the script does not work with no error in console. How do I convert the innerHTML which comes as a string to the form [40.6976,-74.2598] which can be used.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Check for typeof is "string"
If true — convert the argument value to Object using JSON.parse

function toDMS(n) {
  n = Math.abs(n);
  const d = Math.floor(n);
  n = n - d;
  n *= 60;
  const m = Math.floor(n);
  n = n - m;
  n *= 60;
  const s = Math.floor(n);
  return `${d}° ${m}' ${s}"`;
}

function convertToDMS(latLng) {

  if (typeof latLng === "string") latLng = JSON.parse(latLng);
  const [lat, lng] = latLng;

  const dirLat = lat > 0 ? "N" : "S";
  const dirLng = lng > 0 ? "E" : "W";

  console.log(`${toDMS(lat)} ${dirLat}`);
  console.log(`${toDMS(lng)} ${dirLng}`);
}

convertToDMS([40.6976, -74.2598]);
convertToDMS(document.querySelector("#locationdiv").textContent);
<div id="locationdiv">[40.6976,-74.2598]</div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda