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?

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.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With