Out of my depth a little :-) How do I create a pair of longitude and latitudes to create a polygon showing a 2 mile radius of a particular longitude and latitude? in Java Android. I know how to find your current longitude and latitude but I just can't figure out the above.

My code so far is below:

    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    double longitude = location.getLongitude();
    double latitude = location.getLatitude();

Dani AI

Generated

A practical approach is to generate N “destination” points around the center using the spherical destination‑point formula (true geodesic math). This produces accurate latitude/longitude pairs for a 1‑ or 2‑mile circle regardless of latitude. As pointed out, some mapping APIs can draw a circle from center+radius; if explicit coordinates are required (polygon, export, server checks), the method below yields a polygon approximation with controllable smoothness. This follows the same sampling idea mentioned by but uses the correct spherical math rather than a flat-degree approximation.

// Returns a list of LatLng points approximating a circle around (centerLat, centerLon)
// radiusMeters = radius in meters (1 mile = 1609.344 m, 2 miles = 3218.688 m)
// numPoints = number of vertices to generate (>=3). Use 32..128 depending on smoothness.
public static List<com.google.android.gms.maps.model.LatLng> createCircle(
        double centerLat, double centerLon, double radiusMeters, int numPoints) {
    final double R = 6371000.0; // Earth radius in meters (mean)
    int pts = Math.max(3, numPoints);
    double latRad = Math.toRadians(centerLat);
    double lonRad = Math.toRadians(centerLon);
    double angular = radiusMeters / R;
    List<com.google.android.gms.maps.model.LatLng> result = new ArrayList<>(pts + 1);

    for (int i = 0; i <= pts; i++) { // <= to close polygon
        double bearing = 2.0 * Math.PI * i / pts;
        double lat2 = Math.asin(Math.sin(latRad) * Math.cos(angular)
                + Math.cos(latRad) * Math.sin(angular) * Math.cos(bearing));
        double lon2 = lonRad + Math.atan2(Math.sin(bearing) * Math.sin(angular) * Math.cos(latRad),
                Math.cos(angular) - Math.sin(latRad) * Math.sin(lat2));
        double latDeg = Math.toDegrees(lat2);
        double lonDeg = Math.toDegrees(lon2);
        lonDeg = ((lonDeg + 540.0) % 360.0) - 180.0; // normalize to [-180,180)
        result.add(new com.google.android.gms.maps.model.LatLng(latDeg, lonDeg));
    }
    return result;
}

Notes and tips:

  • For visual smoothness: 32 points = coarse, 64 = good, 128+ = very smooth but heavier to render.
  • Use the precise meter conversions shown above (1 mile = 1609.344 m). For a 2‑mile radius use 3218.688 m.
  • For small radii at mid latitudes, a planar sin/cos + degree scaling (the simpler method suggested) is OK, but the spherical formula avoids growing error near poles or for larger radii.
  • If the goal is geofencing, Android’s Geofence API accepts center+radius directly; if the goal is just drawing on Google Maps, GoogleMap.addCircle is simpler than a polygon.

Recommended Answers

All 14 Replies

for a resource.

Also Here is another resource.

Here is another.

Those resources don't really help (as from what I understand of Haversine formula, it requires 2 sets of longitudes and latitudes from the onset. Which is what I'm trying to attain via this question) do you personally have any opinions or skills in this field which can help?

I'm no expert, but maybe this will help:
There's a simple formula for the distance between two long/lat points (Google!). You can rearrange that that to make the distance fixed and either the second long or the second lat the unknown. This will give you four new long/lat points, N.S.E.amd W of the original by the exact distance - thus defining a polygon. With a bit more math you can solve it to get the four points NE,NW.SE and SW, and with a bit more math again any of the infinite number of points that lie on your circle.
If I'm missing yout point here, just ignore this.

I apologise, I've made a mistake. There should actually three pairs of longitude and latitude coordinates to form a radius of 2 miles around the user's location expressed earlier. Hopefully this helps. sorry again :-)

So that's any three points on the circle? Eg 2 miles North, 2 miles South 2 miles East of the location?

No, I'm asking how I figure out the longitude and latitude coordinates necessary to create a 1 mile radius around the user's location?(expressed above) I don't mind how many coordinates there it takes, but I'd like to create a circular radius around one location.

You need to decide how "smooth" you want your circle to be if you are approximating it by a polygon. Three points is the minimum to define a 2D area, but a smooth circle may require dozens to hundreds of points. Is that really where you want to go?
You don't describe the application, but do you not have access to a method that draws circles?

Do you know how to accomplish this (within java, Android), regardless how many points there ends up being?

Application (what I'm wanting to accomplish):

  1. System identifies user location in longitude and latitude (as above)

  2. System then utilises user location ( longitude and latitude) to form a radius of 2 miles from user's location by means of a polygon parameter expressed as multiple pairs of longitude and latitude coordinates

Actually, mathematically a circle is defined by its center and radius so that's all you need to know to store the data.
I wouldn't be at all surprised if the graphical API you're using has a method that will draw a circle on screen using those same things. And then it's just a question of converting the data you have to the coordinate system required for the API (it won't need geographic coordinates and miles but pixel coordinates and number of pixels), which is a scaling problem you'll have to write something for (or which might exist as part of that same API if it's intended for geomapping).

^ that's what I thought too, and I asked him if he had any circle-drawing methods available, but his answer is perfectly clear: "by means of a polygon parameter expressed as multiple pairs of longitude and latitude coordinates". So I guess that's that.

The api doesn't seem to offer much help in that regard.
I mean no disrespect. Although what has been mentioned within this thread, has been insightful. As of yet no one has suggested any Java code which could help me solve this problem. I don't expect anyone to do all the work for me, but I hoped someone would be able to help with the coding. Which is why I've come to a Java forum, is anyone able to help in this fashion?

Maybe a misunderstanding? This is a forum where people can come for help with their Java code. It's not a free coding service.
I posted some formulae and algorithms that should solve your problem as stated. If you have difficulty coding that then post what you have been able to do, and explain exactly what is blocking your progress.

hmm, "no one has posted the code" followed directly with "I expect no one to do my work for me". That's contradicting yourself...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.