i want to convert google map pixels to kilometers

2.5k views Asked by At

hi friends in my gps application i did convert Geo point to pixels. i have two geo points so i convert this both of two points to pixels now i take different of this both pixels points and i want to convert this difference to kilometers

1

There are 1 answers

2
Tovi7 On BEST ANSWER

I wouldn't recommend using the view pixels to do distance calculation. If you have the geo points, you should use those. It all comes down to some geodetic calculation. And the accuracy depends on how you model the earth. What you want is to use geodetic great circle lines to perform distance calculations.

If you model the earth as a sphere (using law of cosines):

double earthAverageRadius = 6378137; //Average mean in meters when modeling the world as a sphere
double angle = Math.acos(Math.sin(point1.x) * Math.sin(point2.x)
                  + Math.cos(point1.x) * Math.cos(point2.x) * Math.cos(point1.y- point2.y));
double distance = angle * pi * earthAverageRadius; // distance in metres

I would also recommend looking into the Haversine formula, which is numerically more stable. Using the haversine formula, the angle calculated in the previous code would be:

double a = Math.pow(Math.sin((point2.x-point1.x)/2.0), 2.0)
                 + Math.cos(point1.x) * Math.cos(point2.x) * Math.pow(Math.sin((point2.y-point1.y)/2.0), 2.0);
double angle = 2 * Math.asin(Math.min(1.0, Math.sqrt(a)));

If you want increased accuracy (for large distances), you should consider modeling the earth as an ellipsoid, though the calculations for this are considerably harder.

EDIT: Also note that the above only holds if you give longitude and latitude in radians. So you'll have to make that conversion first too.