0

Does anyone know where the separate latitude and longitude distances can be found between two google markers? I can get the total distance using the haversine method such as this: Find distance between two points on map using Google Map API V2

How can I get each value please?

Thanks

Frank

Community
  • 1
  • 1
AesculusMaximus
  • 195
  • 3
  • 14
  • Wouldn't it just be something like `Math.abs(loc1.getLatitude() - loc2.getLatitude())` where `loc1` and `loc2` are your locations? – Kevin Cronly Jun 22 '15 at 19:49

1 Answers1

0

The Marker class has a Position property, so

double latitudeDifferenceInDegrees = marker1.getPosition().latitude - marker2.getPosition().latitude;
double longitudeDifferenceInDegrees = marker1.getPosition().longitude - marker2.getPosition().longitude;

You can convert the latitude difference into a distance in meters - each degree is equal to 110.574 kilometers:

double latitudeDifferenceInMeters = latitudeDifferenceInDegrees * 110574;

You can't do the same for longitude. On the equator, a longitude degree is about 111 km, but near the poles, it is much less. If you are only using this for short distances, I'd go for

double longitudeDifferenceInMeters = longitudeDifferenceInDegrees * 111320 * Math.cos((marker1.getPosition().latitude + marker2.getPosition().latitude) / 2);

Note that both can give negative values if the first marker lies south or west of the second marker. If you're interested in positive values only, apply Math.abs().

Community
  • 1
  • 1
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
  • Thanks Glorfindel - This looks nice and easy for a novice like me. Ill give her a whirl – AesculusMaximus Jun 22 '15 at 20:11
  • to Giorfinei: Longitude is horizontal (X axis), Latitude is vetical (Y axis) and is the latitude which is different moving far from the equator. That difference is due to Mercator correction and is because you see a squared world and not a rectangular one. You can find further details at "scale factor" https://en.wikipedia.org/wiki/Mercator_projection – N Dorigatti Jun 22 '15 at 20:34
  • @NDorigatti no, that's not true at all. This has nothing to do with Mercator - the formulas I use (and the haversine formula mentioned by the OP) are coming from [spherical geometry](https://en.wikipedia.org/wiki/Spherical_trigonometry). There *is* a (rather small) inaccuracy in them because the Earth is not a sphere but an ellipsoid. – Glorfindel Jun 22 '15 at 20:38
  • Maybe a [Lambert projection](http://www.emapsworld.com/world-north-pole-lambert-azimuthal-equal-area-projection-map.html) will convince you that longitude degrees are smaller (measured in meters) near the poles ... – Glorfindel Jun 22 '15 at 20:50