1

I have two points on a map -

val point1 : LatLng(13.3016139,77.4219107)
val point2 : LatLng(14.1788932,77.7613413)

I want to calculate and find 100 equidistant points along a straight line between these two coordinates. How do I do that?

ps. I'm sure this has been asked before, I just can't find it.

vepzfe
  • 4,217
  • 5
  • 26
  • 46

2 Answers2

0

Equidistant, and more importantly, straight by which projection?

usually, to find a distance in cartesian space one would use something like the Haversine formula to find a value, as previously answered in stack answer: How to convert latitude or longitude to meters?

As for the equidistant part, once you have the distance decided as per your taste of the shape and radius of Earth at given points, a simple division will do. .

python 3.7
>>> dist = 5427 #just some number
>>> nbr_o_points = 101 
>>> points = [(dist/nbr_o_points)*(i+1) for i in range(nbr_o_points)]
>>> [f'{p:.2f}' for p in points]
['53.73', '107.47', '161.20',..., '5319.53', '5373.27', '5427.00']

Now to transfer these distances from point a to b back onto the desired projection... This is not part of your question... Stack - how-to-determine-vector-between-two-lat-lon-points might help.

take the vector and multiply by the dists in points in order to get your coordinates.

  • Thanks for that answer, but this is not the code I'm looking for. This only outputs a list of equidistant distance values divided by number of point. The 2nd link you sent will help though. Using that and the Haversine formula we can find those coordinates. – vepzfe Jul 18 '19 at 19:54
0

This is how I solved it -

fun findEquidistantPoints(latLng1: LatLng, latLng2: LatLng, pointCount: Int): ArrayList<LatLng> {

    if (pointCount < 0)
        throw IllegalArgumentException("PointCount cannot be less than 0")

    val points = ArrayList<LatLng>()

    val displacement = latLng1.displacementFromInMeters(latLng2)
    val distanceBetweenPoints = displacement / (pointCount + 1)

    for (i in 1..pointCount) {
        val t = (distanceBetweenPoints * i) / displacement

        points.add(LatLng(
                (1 - t) * latLng1.latitude + t * latLng2.latitude,
                (1 - t) * latLng1.longitude + t * latLng2.longitude
        ))
    }

    return points
}
vepzfe
  • 4,217
  • 5
  • 26
  • 46