2

I'm using a Java program I made, to act as a client for rest platform called Firebase.

I have also an android app to read to coordinates from the Firebase platform, to update markers on google maps.

So my question is, if I have the latitude and longitude of both point A and point B, how can I calculate the coordinates between those both points?

Imagine this case scenario

  • Point A (left) lat: 39.091868 long: -9.263187
  • Point B (right) lat: 39.089815 long: -9.261857

enter image description here

How can I calculate, for example, 10 coordinates between those points?

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
James
  • 3,580
  • 3
  • 25
  • 36

3 Answers3

2

If I understand correctly, you want the Midpoint (point exactly at the middle of the two locations in a straight line).

This web page is very helpful for all that sort of things. http://www.movable-type.co.uk/scripts/latlong.html

In Android it would look something like this:

public LatLng midPoint(LatLng A, LatLng B) {
    double phi1 = Math.toRadians(A.latitude);
    double gamma1 = Math.toRadians(A.longitude);

    double phi2 = Math.toRadians(B.latitude);
    double deltaGamma = Math.toRadians(B.longitude - A.longitude);

    double aux1 = Math.cos(phi2) * Math.cos(deltaGamma);
    double aux2 = Math.cos(phi2) * Math.sin(deltaGamma);

    double x = Math.sqrt((Math.cos(phi1) + aux1) * (Math.cos(phi1) + aux1) + aux2 * aux2);
    double y = Math.sin(phi1) + Math.sin(phi2);
    double phi3 = Math.atan2(y, x);

    double gamma3 = gamma1 + Math.atan2(aux2, Math.cos(phi1) + aux1);

    // normalise to −180..+180°
    return new LatLng(Math.toDegrees(phi3), (Math.toDegrees(gamma3) + 540) % 360 - 180); 
}
jpvillegas
  • 93
  • 1
  • 9
2

Please try interpolate() method via spherical geometry.

As discussed in Calculate distances, areas and headings via spherical geometry, using the spherical geometry utilities in SphericalUtil, you can compute distances, areas, and headings based on latitudes and longitudes. As described, this method is:

  • interpolate() – Returns the latitude/longitude coordinates of a point that lies a given fraction of the distance between two given points.

Additional information and sample codes given in this blog might also help you with the implementation part.

Teyam
  • 7,686
  • 3
  • 15
  • 22
-1

If you want those coordinates in straight line between those two points, you can just do some simple math calculations...

If you want those points to be on the road you have to play with Google Directions API.

Rybzor
  • 183
  • 3
  • 12