[GIS] How to get Lat/Long between start and end coordinates and spaced at equal distance

coordinateslatitude longitude

Suppose I have two GPS points (start and end points) each with lat/lon. I want to get equally spaced GPS points between start and end nodes. That is, I want to find GPS points for example every 10 meter from start node to end node.

Similar question is asked here.
From the answers given I have referred the one by Vikram Gupta. This solution works fine but if you replace

int dist = (int) d / interval

by

int dist = (int) d

in getLocations(…) method some calculated coordinates lay beyond the end coordinate. How can the code be modified to prevent this?

Best Answer

Here is the answer. Add the following method,

   public static boolean nodeInBetween(Node start, Node end, Node
 newNode) {
        if (start.getLatitude() < end.getLatitude()) {
             if (newNode.getLatitude()>end.getLatitude()) {
                return false;
            }
         } else if (start.getLatitude() > end.getLatitude()) {
             if (newNode.getLatitude() < end.getLatitude()) {
                 >return false;
            }
        >} else if (start.getLongitude() < end.getLongitude()) {
            if (newNode.getLongitude() > end.getLongitude()) {
                return false;
            }
         } else if (start.getLongitude() > end.getLongitude()) {
             if (newNode.getLongitude() < end.getLongitude()) {
                 return false;
             }
         } 
         return true;
     }

and modify part of for loop of the getLocations(...) method as follows

        if (nodeInBetween(start, end, newNode)) {
            coords.add(newNode);
        } else {
            break;
        }

Note that: MockLocation Class is named Node (with getLatitude() and getLongitude() mehtods) in this code