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

129
Views
How to generate a set of geospacial coordinates?

I tried using this:

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)}`);
}

That's ok for random numbers like 39.21988,9.124741 but they might end up not valid coordinates. And I need 100 of them.

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

0

Your latitude range is incorrect - it should be ±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)}`);
}

about 4 years ago · Juan Pablo Isaza Report

0

You might need to provide some initial Latitude and longitude with some radius (in meters)

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))
}

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!