Maison >php教程 >php手册 >验证坐标在某片坐标区域内 php 代码

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

WBOY
WBOYoriginal
2016-10-08 16:04:431133parcourir

之前碰到的这样一个需求,要将公司的服务在地图中显示出来,并将用户每天的访问坐标进行统计看有多少用户是在所能达到的服务范围半径内。

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

<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 验证坐标点是否在某区域内
 * @author xiaoliang 
 * Class validationMap
 </span><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> $nvert; <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> $vertx[<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'] $minX) <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'] $minY) <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'] $minX || <span style="color: #800080;">$pointArray</span>['lng'] > <span style="color: #800080;">$maxX</span> || <span style="color: #800080;">$pointArray</span>['lat'] $minY || <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>));

在地图中的运用:

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn