>php教程 >php手册 >좌표가 특정 좌표 영역 내에 있는지 확인하십시오. PHP 코드

좌표가 특정 좌표 영역 내에 있는지 확인하십시오. PHP 코드

WBOY
WBOY원래의
2016-10-08 16:04:431131검색

이전에 제가 접한 요구사항은 회사의 서비스를 지도에 표시하고, 사용자의 일일 액세스 좌표를 계산하여 도달 가능한 서비스 반경 내에 몇 명의 사용자가 있는지 확인하는 것이었습니다.

다음은 PHP 코드의 구현입니다(좌표가 특정 좌표 영역 내에 있는지만 확인)

<?<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 验证坐标点是否在某区域内
 * @author xiaoliang <1058436713@qq.com>
 * Class validationMap
 <span style="color: #008000;">*/</span>
<span style="color: #0000ff;">class</span><span style="color: #000000;"> validationMap{
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$coordArray</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$vertx</span> =<span style="color: #000000;"> [];
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$verty</span> =<span style="color: #000000;"> [];
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 设置坐标区域
     * @param mixed $coordArray
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> setCoordArray(<span style="color: #0000ff;">array</span> <span style="color: #800080;">$coordArray</span><span style="color: #000000;">)
    {
        self</span>::<span style="color: #800080;">$coordArray</span> = <span style="color: #800080;">$coordArray</span><span style="color: #000000;">;
    }
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 验证区域范围
     * @param array $coordArray
     * @return bool
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> isCityCenter(<span style="color: #0000ff;">array</span> <span style="color: #800080;">$coordArray</span><span style="color: #000000;">){
        </span><span style="color: #0000ff;">if</span>(!self::vaildatePoint(<span style="color: #800080;">$coordArray</span><span style="color: #000000;">)){
            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
        }
        </span><span style="color: #0000ff;">return</span> self::pnpoly(<span style="color: #008080;">count</span>(self::<span style="color: #800080;">$coordArray</span>), <span style="color: #800080;">$coordArray</span>['lng'], <span style="color: #800080;">$coordArray</span>['lat'<span style="color: #000000;">]);
    }
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 比较区域坐标
     * @param $nvert
     * @param $testx
     * @param $testy
     * @return bool
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> pnpoly(<span style="color: #800080;">$nvert</span>,<span style="color: #800080;">$testx</span>, <span style="color: #800080;">$testy</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$c</span> = <span style="color: #0000ff;">false</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span> = 0, <span style="color: #800080;">$j</span> = <span style="color: #800080;">$nvert</span>-1; <span style="color: #800080;">$i</span> < <span style="color: #800080;">$nvert</span>; <span style="color: #800080;">$j</span> = <span style="color: #800080;">$i</span>++<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">if</span> ( ( (self::<span style="color: #800080;">$verty</span>[<span style="color: #800080;">$i</span>]><span style="color: #800080;">$testy</span>) != (self::<span style="color: #800080;">$verty</span>[<span style="color: #800080;">$j</span>]><span style="color: #800080;">$testy</span>) ) && (<span style="color: #800080;">$testx</span> < (self::<span style="color: #800080;">$vertx</span>[<span style="color: #800080;">$j</span>]-self::<span style="color: #800080;">$vertx</span>[<span style="color: #800080;">$i</span>]) * (<span style="color: #800080;">$testy</span>-self::<span style="color: #800080;">$verty</span>[<span style="color: #800080;">$i</span>]) / (self::<span style="color: #800080;">$verty</span>[<span style="color: #800080;">$j</span>]-self::<span style="color: #800080;">$verty</span>[<span style="color: #800080;">$i</span>]) + self::<span style="color: #800080;">$vertx</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">]) )
                </span><span style="color: #800080;">$c</span> = !<span style="color: #800080;">$c</span><span style="color: #000000;">;
        }
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$c</span><span style="color: #000000;">;
    }
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 验证坐标
     * @param array $pointArray
     * @return bool
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> vaildatePoint(<span style="color: #0000ff;">array</span> <span style="color: #800080;">$pointArray</span><span style="color: #000000;">){
        </span><span style="color: #800080;">$maxY</span> = <span style="color: #800080;">$maxX</span> = 0<span style="color: #000000;">;
        </span><span style="color: #800080;">$minY</span> = <span style="color: #800080;">$minX</span> = 9999<span style="color: #000000;">;
        </span><span style="color: #0000ff;">foreach</span> (self::<span style="color: #800080;">$coordArray</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$item</span><span style="color: #000000;">){
            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$item</span>['lng']><span style="color: #800080;">$maxX</span>) <span style="color: #800080;">$maxX</span> = <span style="color: #800080;">$item</span>['lng'<span style="color: #000000;">];
            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$item</span>['lng'] < <span style="color: #800080;">$minX</span>) <span style="color: #800080;">$minX</span> = <span style="color: #800080;">$item</span>['lng'<span style="color: #000000;">];
            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$item</span>['lat']><span style="color: #800080;">$maxY</span>) <span style="color: #800080;">$maxY</span> = <span style="color: #800080;">$item</span>['lat'<span style="color: #000000;">];
            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$item</span>['lat'] < <span style="color: #800080;">$minY</span>) <span style="color: #800080;">$minY</span> = <span style="color: #800080;">$item</span>['lat'<span style="color: #000000;">];
            self</span>::<span style="color: #800080;">$vertx</span>[] = <span style="color: #800080;">$item</span>['lng'<span style="color: #000000;">];
            self</span>::<span style="color: #800080;">$verty</span>[] = <span style="color: #800080;">$item</span>['lat'<span style="color: #000000;">];
        }
        </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$pointArray</span>['lng'] < <span style="color: #800080;">$minX</span> || <span style="color: #800080;">$pointArray</span>['lng'] > <span style="color: #800080;">$maxX</span> || <span style="color: #800080;">$pointArray</span>['lat'] < <span style="color: #800080;">$minY</span> || <span style="color: #800080;">$pointArray</span>['lat'] > <span style="color: #800080;">$maxY</span><span style="color: #000000;">) {
            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
        }
        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
    }
}<br>/**************************** test *************************************/
</span><span style="color: #800080;">$map</span> = [  <span style="color: #008000;">//</span><span style="color: #008000;">上海</span>
    ["lng" => 121.488286, "lat" => 31.420147],<span style="color: #000000;">
    [</span>"lng" => 121.702154, "lat" => 31.294828],<span style="color: #000000;">
    [</span>"lng" => 121.780918, "lat" => 31.141157],<span style="color: #000000;">
    [</span>"lng" => 121.782068, "lat" => 30.941157],<span style="color: #000000;">
    [</span>"lng" => 121.492885, "lat" => 30.909931],<span style="color: #000000;">
    [</span>"lng" => 121.22325, "lat" => 30.890099],<span style="color: #000000;">
    [</span>"lng" => 121.161482, "lat" => 31.015526],<span style="color: #000000;">
    [</span>"lng" => 121.076395, "lat" => 31.226239],<span style="color: #000000;">
    [</span>"lng" => 121.189873, "lat" => 31.339688],<span style="color: #000000;">
    [</span>"lng" => 121.459509, "lat" => 31.41368],<span style="color: #000000;">
];
</span><span style="color: #800080;">$array</span> = ["lat"=>31.218681,"lng"=>121.08604];<span style="color: #008000;">//</span><span style="color: #008000;">进行验证的区域</span>
validationMap::setCoordArray(<span style="color: #800080;">$map</span><span style="color: #000000;">);
</span><span style="color: #008080;">var_dump</span>(validationMap::isCityCenter(<span style="color: #800080;">$array</span>));

지도에서 사용:

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.