Question

I have the top left lat/lon and bottom right lat/lon for my bounding box.

How do I determine whether a given lat/lon is within that bounding box?

Here is the bounding box I'm working with: Top left lat: 51.780586 Top left lon: -0.727844 Bottom right lat: 51.260196 Bottom right lon: 0.590515

My point is: Lat: 51.508039 Lon: -0.128069

I'm going round circles with this at the moment, any help would be appreciated.

Was it helpful?

Solution

From your description the top left and bottom right corners are generally referred to as the Northwest (NW) and Southeast (SE) corners of a bounding box. Determining if a point is inside is simply checking if the latitude and longitude are within the outer latitude and longitude of the bounding box. Below is psuedo code (where NW.Lat is the latitude of the NW corner, etc).

If ( ( Lat <= NW.Lat && Lat >= SE.Lat ) &&
     ( Lon >= NW.Lon && Lon <= SE.Lon ) )
{
    // The point is in the box
}

OTHER TIPS

Given Latitude = x and Longitude = y

You have x1 and x2. Isn't this simply a matter of verifying that your given x falls between those values, as well as your given y? Because your bounding box is simply a square.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top