Home  >  Q&A  >  body text

How to check if coordinates are inside bounds object in PHP?

I have some coordinates and I want to check if these coordinates are exactly within the Google Maps range (Northeast and South West).

Example:

Available Lat: 40.712776
Available Long: -74.005974

I want to know if the coordinates above are within the bounds below:

> bounds_north_east[lat]    28.63713261089665
> bounds_north_east[lng]    77.22579752272338
> bounds_south_west[lat]    28.619767323553265
> bounds_south_west[lng]    77.18275343245239

I want to achieve this using only PHP script.

P粉101708623P粉101708623220 days ago1499

reply all(1)I'll reply

  • P粉190883225

    P粉1908832252024-04-05 13:43:10

    This helps me. It returns true or false.

    function inBounds($pointLat, $pointLong, $boundsNElat, $boundsNElong, $boundsSWlat, $boundsSWlong) {
        $eastBound = $pointLong < $boundsNElong;
        $westBound = $pointLong > $boundsSWlong;
    
        if ($boundsNElong < $boundsSWlong) {
            $inLong = $eastBound || $westBound;
        } else {
            $inLong = $eastBound && $westBound;
        }
    
        $inLat = $pointLat > $boundsSWlat && $pointLat < $boundsNElat;
        return $inLat && $inLong;
    }

    reply
    0
  • Cancelreply