【问题标题】:How to figure out if a location is within a square (lat,lon) PHP如何确定一个位置是否在一个正方形(纬度,经度)PHP内
【发布时间】:2015-12-03 12:30:56
【问题描述】:

你怎么知道是不是这样:

$Now = array(
'lat' => '59.565423',
'long' => '7.347268'
);

在里面:

$x = array(
'x1' => '59.570281',  // point 1, north west - lat
'y1' => '7.341667',   // ^^^^^^ - lon
'x2' => '59.570281',  // point 2, north east - lat
'y2' => '7.351087',   // ^^^^^^ - lon
'x3' => '59.568195',  // point 3, south east - lat
'y3' => '7.351087',   // ^^^^^^ - lon
'x4' => '59.568195',  // point 4, south west - lat
'y4' => '7.341667'    // ^^^^^^ - lon
 );

如果在内部返回 1,如果在外部返回 0。

【问题讨论】:

  • 你需要更精确 - 你想输出一个包含给定范围内坐标的数组吗?它们不太可能完全匹配。下一个问题 - x 是什么?纬度还是经度?并且(同样)y 是什么?
  • @Matt 这是我自己回答的一个问题(因为我听到其他人问过这个问题)我给出了下面的答案。
  • 对不起,我没有注意到 - 问题(没有你的答案)出现在堆栈溢出审查工具的分类视图中,所以我没有在那里阅读答案。在这种情况下,请无视我的要求。顺便提一句。我给了你一个+1的答案。 :-)
  • 我建议您更改令人困惑的数组结构,数组或数组内的对象有什么问题? $x = array('nw' => array(59.570281, 7.341667)...)
  • @Martin Baker,见我上面的评论。一旦你有这个问题,这个数组就有意义了。

标签: php html math geolocation


【解决方案1】:

方法如下:

  • 首先您需要检查经度是否在您的 |---| 范围内区域如此:

    if($Now['long'] > $x['y1'] && $Now['long'] < $x['y2'])

    • 那么你需要检查纬度是否在参数范围内:

    if($Now['lat'] < $x['x1'] && $Now['lat'] > $x['x4'])

全功能:

function InsideOrNot($Now, $x){
    //If 0 is within |----| (|--0--|)
    if($Now['long'] > $x['y1'] && $Now['long'] < $x['y2']){

        //if 0 is within ___
        //                |
        //               ___   
        if($Now['lat'] < $x['x1'] && $Now['lat'] > $x['x4']){
            return 1;
        }

        else{
            return 0;
        }
    }
    else{
        return 0;
    }


}

现在:echo InsideOrNot($Now, $x);

【讨论】:

    猜你喜欢
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    • 2017-08-08
    • 2012-12-01
    • 2014-03-30
    • 1970-01-01
    相关资源
    最近更新 更多