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

有些话、适合烂在心里 提交于 2019-12-11 08:35:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!