如何确定位置是否在正方形(lat,lon)PHP内

时间:2015-12-03 12:30:56

标签: php html5 math geolocation

你怎么能弄明白:

$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。

1 个答案:

答案 0 :(得分: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);