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

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

WBOY
Lepaskan: 2016-10-08 16:04:43
asal
1128 orang telah melayarinya

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

以下是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>));
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