Intenté usar esto:
function getRandomInRange(from, to, fixed) { return parseFloat((Math.random() * (to - from) + from).toFixed(fixed)); } var latLongPairs = 4; for(var i =0; i<latLongPairs; i++) { console.log(`${getRandomInRange(-180, 180, 3)}, ${getRandomInRange(-180, 180, 3)}`); } Eso está bien para números aleatorios como 39.21988, 39.21988,9.124741 pero podrían terminar como coordenadas no válidas. Y necesito 100 de ellos.
Su rango de latitud es incorrecto; debería ser de ±90.
function getRandomInRange(from, to) { return Math.random() * (to - from) + from; } const latLongPairs = 4; for (let i = 0; i < latLongPairs; ++i) { let lat = getRandomInRange(-90, 90); let lon = getRandomInRange(-180, 180); console.log(`${lat.toFixed(3)}, ${lon.toFixed(3)}`); }Es posible que deba proporcionar una latitud y longitud iniciales con un radio (en metros)
var getRandomLocation = function(latitude, longitude, radiusInMeters) { var getRandomCoordinates = function(radius, uniform) { // Generate two random numbers var a = Math.random(), b = Math.random(); // Flip for more uniformity. if (uniform) { if (b < a) { var c = b; b = a; a = c; } } // It's all triangles. return [ b * radius * Math.cos(2 * Math.PI * a / b), b * radius * Math.sin(2 * Math.PI * a / b) ]; }; var randomCoordinates = getRandomCoordinates(radiusInMeters, true); // Earths radius in meters via WGS 84 model. var earth = 6378137; // Offsets in meters. var northOffset = randomCoordinates[0], eastOffset = randomCoordinates[1]; // Offset coordinates in radians. var offsetLatitude = northOffset / earth, offsetLongitude = eastOffset / (earth * Math.cos(Math.PI * (latitude / 180))); return `${latitude + (offsetLatitude * (180 / Math.PI))}, ${longitude + (offsetLongitude * (180 / Math.PI))}` }; for (i = 0; i < 182; i++) { console.log(getRandomLocation(41.8819, -87.6278, 50)) }