问题
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.
回答1:
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.
回答2:
I know that a long time has passedby, but maybe someone still interested.
Double lat_A, lat_B,lng_A, lng_B, curr_lat,curr_lng;
LatLng nw_corner, sw_corner;
Boolean lat_OK,lng_OK,all_OK;
int t_segs, points;
t_segs = segments.size()-1;
curr_lat = curr_LatLng.latitude;
curr_lng = curr_LatLng.longitude;
lat_A = list_segm_points.get(0).latitude;
lng_A = list_segm_points.get(0).longitude;
lat_B = list_segm_points.get(t_segs).latitude;
lng_B = list_segm_points.get(t_segs).longitude;
if (lat_A > lat_B ) {
lat_OK = curr_lat < lat_A && curr_lat > lat_B;
} else {
lat_OK = curr_lat > lat_A && curr_lat < lat_B;
}
if (lng_A > lng_B ) {
lng_OK = curr_lng < lng_A && curr_lng > lng_B;
} else {
lng_OK = curr_lng > lng_A && curr_lng < lng_B;
}
all_OK = lat_OK && lng_OK;
return all_OK;
来源:https://stackoverflow.com/questions/6664069/finding-if-a-given-position-lat-lon-is-within-the-boundingbox-of-two-coordina