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
  • 34,719
  • 24
  • 185
  • 251
user2399158
  • 543
  • 3
  • 8
  • 22
  • 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

1 Answers1

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
  • 543
  • 3
  • 8
  • 22