Search code examples
iphonegeolocationlatitude-longituderoute-me

Finding if a given position (lat, lon) is within the BoundingBox of two coordinates (route-me)


I am working on an iPhone app using the Route-Me project. I am using a method called 'latitudeLongitudeBoundingBoxForScreen' which returns a set of two cooördinates. Defining the Northeast and Southwest corners in lat/lon of your screen. This seems to work fine.

My question is how can I determine whether a given point (lat/lon) is within the boundaries of these two coordinates (northeast,southwest)?

Thank you very much in advance for your help.


Solution

  • For general point in a polygon, the Point Inclusion Test seems to work assuming a trivial Equirectanglular projection (x = lon, y = lat).

    Since you know the north-east and south-west, you can compute the north-west and south-east corners. Also, keep in mind that your bounding polygon (box) must following a winding convention (you just can't throw points at the Point Inclusion Test).

    This will break if the bounding box goes across the 180 lon line. The simple fix is if the box that crosses 180 lon line is to add 360 to the negative longitude. This isn't a valid navigation point but it makes the math work.

    This also will not work around the poles.