Need JavaScript Help - Generate Random Geo Coordinates

askmars

Client
Регистрация
18.05.2017
Сообщения
35
Благодарностей
0
Баллы
6
Hi-
I am new to JavaScript and would like to write a script and run it on Zennoposter to generate a specified number of random geo coordinates within a specified mile radius from a seed point. I want to get a list of Geo points after I run this script.

Below is the JavaScript:
JavaScript:
function generateRandomGeoCoordinates(seedPoint, radiusInMiles, numberOfPoints) {
  // Convert radius to meters
  const radiusInMeters = radiusInMiles * 1609.34;

  // Extract latitude and longitude from seed point
  const seedLat = seedPoint.latitude;
  const seedLong = seedPoint.longitude;

  // Initialize empty list for coordinates
  const coordinates = [];

  for (let i = 0; i < numberOfPoints; i++) {
    // Generate random angle in radians
    const angle = Math.random() * 2 * Math.PI;

    // Generate random distance within radius
    const distance = Math.random() * radiusInMeters;

    // Calculate offset based on angle and distance
    const latOffset = Math.cos(angle) * distance / 111111;
    const longOffset = Math.sin(angle) * distance / (111111 * Math.cos(Math.PI / 180 * seedLat));

    // Add offset to seed point
    const newLat = seedLat + latOffset;
    const newLong = seedLong + longOffset;

    // Ensure coordinates are within valid ranges
    coordinates.push({
      latitude: Math.max(-90, Math.min(90, newLat)),
      longitude: Math.max(-180, Math.min(180, newLong)),
    });
  }

  return coordinates;
}

// Example usage
const seedPoint = { latitude: 37.7749, longitude: -122.4194 }; // San Francisco
const radiusInMiles = 10;
const numberOfPoints = 5;

const randomCoordinates = generateRandomGeoCoordinates(seedPoint, radiusInMiles, numberOfPoints);

console.log(randomCoordinates);
After I run this script on ProjectMaker, it does not retun anything. Please see the screenshot below.
Could you please anything I need to update to make it work?
Thank you so much.

118188
 

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)