Home > php教程 > php手册 > Verify that coordinates are within a certain coordinate area php code

Verify that coordinates are within a certain coordinate area php code

WBOY
Release: 2016-10-08 16:04:43
Original
1127 people have browsed it

I encountered a need before. It was necessary to display the company's services on a map, and to count the daily access coordinates of users to see how many users were within the reachable service radius.

The following is the implementation of the PHP code (only verify that the coordinates are within a certain coordinate area)

<?<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>));
Copy after login

Use in map:

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template