Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Coordinates Between Two Points?

Let's say I have two points: l1 = (lat1, lng1) and l2 = (lat2, lng2). How to programmatically generate a grid of coordinates that are x meters apart?

enter image description here

As per this image, if I have the two red points and a value x, I want to find out the coordinates of the yellow points. I know that the two red points might not have a distance between them (horizontally or vertically) that is a multiple of x, which might lead to the last two points in a row and/or column having a distance less than x.

like image 641
iTurki Avatar asked Jun 22 '26 11:06

iTurki


1 Answers

Based on your map, you are creating a bounded grid mesh over a very small portion of the Earth's surface. That means you can ignore projection math and just work with 2D algebra: subtract longitudes and divide by number of horizontal grids, then subtract latitudes and divide by number of vertical grids.

// source coordinates in decimal degrees
$pOne = [ 'lat' => 35.001234, 'lon' => -78.940202 ];
$pTwo = [ 'lat' => 35.010272, 'lon' => -78.721478 ];

// grid size along latitude and longitude
$nLat = 5;
$nLon = 5;

// get the grid size for each dimension in degrees
$dLat = ($pTwo['lat'] - $pOne['lat']) / $nLat;
$dLon = ($pTwo['lon'] - $pOne['lon']) / $nLat;

for ($i = 0; $i < $nLat; $i++) {
    $lat = $pOne['lat'] + ($i*$dLat);
    for ($j = 0; $j < $nLon; $j++) {
        $lon = $pOne['lon'] + ($j*$dLon);
        printf('<%.6f, %.6f>' . PHP_EOL, $lat, $lon);
    }
}
like image 128
bishop Avatar answered Jun 25 '26 02:06

bishop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!