3

I am writing a java program to generate a all the longitude and latitude with a fixed distance from my my given point. the distance must be exact 2000km, not withub 2000km.

this is my code

public static void getLocation(double x0, double y0, int meters) {
        Random random = new Random();

        // Convert radius from meters to degrees
        double radiusInDegrees = meters / 111000f;

        double u = random.nextDouble();
        double v = random.nextDouble();
        double w = radiusInDegrees * Math.sqrt(u);
        double t = 2 * Math.PI * v;
        double x = w * Math.cos(t);
        double y = w * Math.sin(t);

        // Adjust the x-coordinate for the shrinking of the east-west distances
       // double new_x = x / Math.cos(Math.toRadians(y0));

        double foundLongitude = x + x0;
        double foundLatitude = y + y0;
        System.out.println("Longitude: " + foundLongitude + "  Latitude: " + foundLatitude );
    } 

How do I make all the point generate equal distance from the geo point, like forming a circle?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user2399158
  • 561
  • 3
  • 10
  • 26
  • let say we have geo point with coordinates X,Y and radius R. you want to get the coordinates of points along the entire perimeter of the circle, correct ? – Z.R.T. Dec 04 '17 at 02:25
  • yupppp........... – user2399158 Dec 04 '17 at 02:37
  • Pick a random bearing then use a distance-and-bearing formula to calculate the destination lat/long. [This answer](https://stackoverflow.com/questions/10119479/calculating-lat-and-long-from-bearing-and-distance) has one solution. Some more details [here](https://www.movable-type.co.uk/scripts/latlong.html). – teppic Dec 04 '17 at 02:54
  • Thanksssssssssss – user2399158 Dec 04 '17 at 04:48

2 Answers2

1
public static void generatePoint(double latitude, double longitude, double distanceInMetres, double bearing) {
        Random random = new Random();

        //int bear = random.nextInt(360);
        double brngRad = Math.toRadians(bearing);
        double latRad = Math.toRadians(latitude);
        double lonRad = Math.toRadians(longitude);
        int earthRadiusInMetres = 6371000;
        double distFrac = distanceInMetres / earthRadiusInMetres;

        double latitudeResult = Math.asin(Math.sin(latRad) * Math.cos(distFrac) + Math.cos(latRad) * Math.sin(distFrac) * Math.cos(brngRad));
        double a = Math.atan2(Math.sin(brngRad) * Math.sin(distFrac) * Math.cos(latRad), Math.cos(distFrac) - Math.sin(latRad) * Math.sin(latitudeResult));
        double longitudeResult = (lonRad + a + 3 * Math.PI) % (2 * Math.PI) - Math.PI;

        System.out.println("bearing: "+bearing+ ", latitude: " + Math.toDegrees(latitudeResult) + ", longitude: " + Math.toDegrees(longitudeResult));
    }

need to add bearing

user2399158
  • 561
  • 3
  • 10
  • 26
1

For anyone looking, this is how I implemented this code in JavaScript:

function generatePoint(
  latitude,
  longitude,
  distanceInMetres,
  bearing = Math.floor(Math.random() * (360 - 1) + 1)
) {
  const brngRad = deg2Rad(bearing);
  const latRad = deg2Rad(latitude);
  const lonRad = deg2Rad(longitude);

  const EARTH_RADIUS_IN_METRES = 6371000;

  const distFrac = distanceInMetres / EARTH_RADIUS_IN_METRES;

  const latitudeResult = Math.asin(
    Math.sin(latRad) * Math.cos(distFrac) +
      Math.cos(latRad) * Math.sin(distFrac) * Math.cos(brngRad)
  );
  const a = Math.atan2(
    Math.sin(brngRad) * Math.sin(distFrac) * Math.cos(latRad),
    Math.cos(distFrac) - Math.sin(latRad) * Math.sin(latitudeResult)
  );

  const longitudeResult =
    ((lonRad + a + 3 * Math.PI) % (2 * Math.PI)) - Math.PI;

  return {
    latitude: rad2Deg(latitudeResult),
    longitude: rad2Deg(longitudeResult),
    bearing,
  };
}

function deg2Rad(deg) {
  return deg * (Math.PI / 180);
}

function rad2Deg(rad) {
  return rad * (180 / Math.PI);
}

It will also generate a random bearing if none is provided

urboypx
  • 21
  • 3