I have a MapView that I'm displaying a "useful radius" (think accuracy of coordinate) in. Using MapView
's Projection's metersToEquatorPixels, which is admittedly just for equatorial distance) isn't giving me an accurate enough distance (in pixels). How would you compute this if you wanted to display a circle around your coordinate, given radius?
Asked
Active
Viewed 1.2k times
12

Jeremy Logan
- 47,151
- 38
- 123
- 143
-
2This could also, probably, be stated as: I have a GPS coordinate and a distance, how can I draw a circle that encompasses the radius from the GPS point? – Jeremy Logan Jan 16 '10 at 10:59
-
I'm not sure I understand the problem - what do you mean when you say that it's not giving you an accurate enough distance? That you meant for it say 100m but it only shows a circle at 50m or something? – Dan Lew Jan 16 '10 at 14:40
-
Yes. It's off by a significant amount. – Jeremy Logan Jan 17 '10 at 03:38
-
Actually the metersToRadius function fails badly, I tested it by using a mapview and drawing a circle when clicking a location. I'm not good in math, the formula needs an expert ;) – Apr 02 '11 at 01:34
3 Answers
29
So, Google Maps uses a Mercator projection. This means that the further you get from the equator the more distorted the distances become. According to this discussion, the proper way to convert for distances is something like:
public static int metersToRadius(float meters, MapView map, double latitude) {
return (int) (map.getProjection().metersToEquatorPixels(meters) * (1/ Math.cos(Math.toRadians(latitude))));
}

Jeremy Logan
- 47,151
- 38
- 123
- 143
-
Thank you for the answer. But I had a query about this equation. Is this equation heavy enough to slow the performance of an app? Just curious. – Ahmed Faisal Sep 28 '11 at 15:08
-
2The only possibly time consuming part of the formula is the Math.cos(). You can use the android.util.FloatMath.cos() function to speed things up, or even keep a table of precomputed factors, e.g. for every full degree. Then you could just say return (int) (map.getProjection().metersToEquatorPixels(meters) * factor[(int)latitude]. – Ridcully Sep 30 '11 at 08:01
0
mMap.addCircle(
new CircleOptions().center(
new LatLng(
bounds.getCenter().latitude,
bounds.getCenter().longitude
)
)
.radius(50000)
.strokeWidth(0f)
.fillColor(0x550000FF)
);

Jeremy Logan
- 47,151
- 38
- 123
- 143

Mohan Kumar
- 21
- 1
-
2The answer requires formatting and add a bit more context on how your code works and how does it solve the problem of OP! – abhiarora Feb 07 '17 at 11:19
-2
projection.toPixels(GeoP, pt);
float radius = projection.metersToEquatorPixels(50);
Try this and see... i used it on my MapRadius and it seems to be working

Loshi
- 221
- 1
- 5
- 19
-
If you see my answer you'll see exactly why this won't work in all cases. – Jeremy Logan Oct 19 '12 at 16:16