Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
Convierta Sting a las coordenadas [x, y] en 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>

Usando el script anterior, puedo obtener grados, minutos y segundos como 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;

el script no funciona sin ningún error en la consola. ¿Cómo convierto el HTML interno que viene como una cadena al formulario [40.6976, -74.2598] que se puede usar?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Compruebe que typeof es "string"
Si es verdadero, convierta el valor del argumento en Objeto usando 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!