欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  类库下载

验证坐标在某片坐标区域内 php 代码

程序员文章站 2022-03-14 10:55:48
...
以下是PHP代码的实现 (仅验证坐标在某片坐标区域内)

 * Class validationMap
 */
class validationMap{
    private static $coordArray;
    private static $vertx = [];
    private static $verty = [];
    /**
     * 设置坐标区域
     * @param mixed $coordArray
     */
    public static function setCoordArray(array $coordArray)
    {
        self::$coordArray = $coordArray;
    }
    /**
     * 验证区域范围
     * @param array $coordArray
     * @return bool
     */
    public static function isCityCenter(array $coordArray){
        if(!self::vaildatePoint($coordArray)){
            return false;
        }
        return self::pnpoly(count(self::$coordArray), $coordArray['lng'], $coordArray['lat']);
    }
    /**
     * 比较区域坐标
     * @param $nvert
     * @param $testx
     * @param $testy
     * @return bool
     */
    private static function pnpoly($nvert,$testx, $testy)
    {
        $c = false;
        for ($i = 0, $j = $nvert-1; $i $testy) != (self::$verty[$j]>$testy) ) && ($testx $maxX) $maxX = $item['lng'];
            if($item['lng'] $maxY) $maxY = $item['lat'];
            if($item['lat']  $maxX || $pointArray['lat']  $maxY) {
            return false;
        }
        return true;
    }
}
/**************************** test *************************************/
$map = [  //上海
    ["lng" => 121.488286, "lat" => 31.420147],
    ["lng" => 121.702154, "lat" => 31.294828],
    ["lng" => 121.780918, "lat" => 31.141157],
    ["lng" => 121.782068, "lat" => 30.941157],
    ["lng" => 121.492885, "lat" => 30.909931],
    ["lng" => 121.22325, "lat" => 30.890099],
    ["lng" => 121.161482, "lat" => 31.015526],
    ["lng" => 121.076395, "lat" => 31.226239],
    ["lng" => 121.189873, "lat" => 31.339688],
    ["lng" => 121.459509, "lat" => 31.41368],
];
$array = ["lat"=>31.218681,"lng"=>121.08604];//进行验证的区域
validationMap::setCoordArray($map);
var_dump(validationMap::isCityCenter($array));