Rumah > 类库下载 > PHP类库 > teks badan

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

高洛峰
Lepaskan: 2016-10-08 17:45:55
asal
1448 orang telah melayarinya

以下是PHP代码的实现 (仅验证坐标在某片坐标区域内)

<?php
/**
 * 验证坐标点是否在某区域内
 * @author xiaoliang <1058436713@qq.com>
 * 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[&#39;lng&#39;], $coordArray[&#39;lat&#39;]);
    }
    /**
     * 比较区域坐标
     * @param $nvert
     * @param $testx
     * @param $testy
     * @return bool
     */
    private static function pnpoly($nvert,$testx, $testy)
    {
        $c = false;
        for ($i = 0, $j = $nvert-1; $i < $nvert; $j = $i++) {
            if ( ( (self::$verty[$i]>$testy) != (self::$verty[$j]>$testy) ) && ($testx < (self::$vertx[$j]-self::$vertx[$i]) * ($testy-self::$verty[$i]) / (self::$verty[$j]-self::$verty[$i]) + self::$vertx[$i]) )
                $c = !$c;
        }
        return $c;
    }
    /**
     * 验证坐标
     * @param array $pointArray
     * @return bool
     */
    private static function vaildatePoint(array $pointArray){
        $maxY = $maxX = 0;
        $minY = $minX = 9999;
        foreach (self::$coordArray as $item){
            if($item[&#39;lng&#39;]>$maxX) $maxX = $item[&#39;lng&#39;];
            if($item[&#39;lng&#39;] < $minX) $minX = $item[&#39;lng&#39;];
            if($item[&#39;lat&#39;]>$maxY) $maxY = $item[&#39;lat&#39;];
            if($item[&#39;lat&#39;] < $minY) $minY = $item[&#39;lat&#39;];
            self::$vertx[] = $item[&#39;lng&#39;];
            self::$verty[] = $item[&#39;lat&#39;];
        }
        if ($pointArray[&#39;lng&#39;] < $minX || $pointArray[&#39;lng&#39;] > $maxX || $pointArray[&#39;lat&#39;] < $minY || $pointArray[&#39;lat&#39;] > $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));
Salin selepas log masuk


sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!