Heim >php教程 >php手册 >Stellen Sie sicher, dass die Koordinaten innerhalb eines bestimmten Koordinatenbereichs im PHP-Code liegen

Stellen Sie sicher, dass die Koordinaten innerhalb eines bestimmten Koordinatenbereichs im PHP-Code liegen

WBOY
WBOYOriginal
2016-10-08 16:04:431212Durchsuche

Eine Anforderung, auf die ich zuvor gestoßen bin, bestand darin, die Dienste des Unternehmens auf einer Karte anzuzeigen und die täglichen Zugriffskoordinaten der Benutzer zu zählen, um zu sehen, wie viele Benutzer sich im erreichbaren Serviceradius befinden.

Das Folgende ist die Implementierung des PHP-Codes (überprüfen Sie nur, ob die Koordinaten innerhalb eines bestimmten Koordinatenbereichs liegen)

<?<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>));

In der Karte verwenden:

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn