早睡早起,方能养生
Sleep early rise early, way to keep healthy

PHP地理围栏

2020-11-12 18:20
views 2793

                           

/**
 * 围栏算法,判断坐标是否在围栏里(点越多越准确)
 * @param array $fences 围栏数组 (左上->右上->右下->下) ['113.664673,34.810146','113.681667,34.796896','113.69231,34.794711','113.702009,34.809159']
 * @param string $point 要判断的坐标
 * @return mixed
 */
function in_fences($fences, $point) {
    $nvert = count($fences);
    $vertx = [];
    $verty = [];
    list($testy, $testx) = explode(',', $point);
    foreach ($fences as $r) {
        list($lng, $lat) = explode(',', $r);
        $vertx[] = $lat;
        $verty[] = $lng;
    }
    $i = $j = $c = 0;
    for ($i = 0, $j = $nvert - 1; $i < $nvert; $j = $i++) {
        if (( ($verty[$i] > $testy) != ($verty[$j] > $testy) ) &&
            ($testx < ($vertx[$j] - $vertx[$i]) * ($testy - $verty[$i]) / ($verty[$j] - $verty[$i]) + $vertx[$i]))
            $c = !$c;
    }
    return $c;
}

$array = [
    '113.65416,34.767857',
    '113.733499,34.767857',
    '113.732636,34.7274',
    '113.650711,34.7274',
];

if (in_fences($array, '113.694404,34.751605')) {
    echo 'Inside!';
} else {
    echo 'Not inside!';
}

// Inside!

 

百度坐标拾取系统:

https://api.map.baidu.com/lbsapi/getpoint/index.html

 

腾讯坐标拾取系统:

https://lbs.qq.com/tool/getpoint/index.html

 

高德坐标拾取系统:

https://lbs.amap.com/console/show/picker

 

谷歌坐标拾取系统:

https://www.google.com/maps

 



分享
0 条讨论
top