查找给定的lat / lng坐标是否位于正方形/矩形内

时间:2018-01-03 12:35:53

标签: java google-maps

我有3套坐标

  1. 协调检查
  2. NorthWest坐标
  3. SouthEast坐标
  4. 2& 3是正方形/矩形的对角线。我必须找到给定的坐标(1)是否在2& 3内。

    之前发布了同样的问题,但没有发布答案。 Duplicate Question

1 个答案:

答案 0 :(得分:1)

我得到了我正在寻找的解决方案。 Got solution from here

/*
* top: north latitude of bounding box.
* left: left longitude of bounding box (western bound). 
* bottom: south latitude of the bounding box.
* right: right longitude of bounding box (eastern bound).
* latitude: latitude of the point to check.
* longitude: longitude of the point to check.
*/
boolean isBounded(double top, double left, 
                  double bottom, double right, 
                  double latitude, double longitude){
        /* Check latitude bounds first. */
        if(top >= latitude && latitude >= bottom){
                /* If your bounding box doesn't wrap 
                   the date line the value
                   must be between the bounds.
                   If your bounding box does wrap the 
                   date line it only needs to be  
                   higher than the left bound or 
                   lower than the right bound. */
            if(left <= right && left <= longitude && longitude <= right){
                return true;
            } else if(left > right && (left <= longitude || longitude <= right)) {
                return true;
            }
        }
        return false;
}