JSFiddle - React, Tailwind, and code Playground

by nicolas tenorio

HTML

<div id="d"></div>

JavaScript

var s1;
var s2;
if (navigator.geolocation) {
	
    navigator.geolocation.getCurrentPosition(
    
function showposition(position) {  
  s1 = position.coords.latitude;
  s2 = position.coords.longitude;
 Number.prototype.toRad = function() {
   return this * Math.PI / 180;
};

var lat2 = s1; 
var lon2 = s2; 
var lat1 = 3.467019;  
var lon1 = -76.52398;  



var R = 6371; // km 
//has a problem with the .toRad() method below.
var x1 = lat2-lat1;
var dLat = x1.toRad();  
var x2 = lon2-lon1;
var dLon = x2.toRad();  
var a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
                Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
                Math.sin(dLon/2) * Math.sin(dLon/2);  
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
var d = R * c; 

document.getElementById("d").innerHTML = d;}
		
												); 
  
								} 
else {
  alert("no sirve el gps");
}