The following is the implementation of the PHP code (only verify that the coordinates are within a certain coordinate area)
<?php /** * 驗(yàn)證坐標(biāo)點(diǎn)是否在某區(qū)域內(nèi) * @author xiaoliang <1058436713@qq.com> * Class validationMap */ class validationMap{ private static $coordArray; private static $vertx = []; private static $verty = []; /** * 設(shè)置坐標(biāo)區(qū)域 * @param mixed $coordArray */ public static function setCoordArray(array $coordArray) { self::$coordArray = $coordArray; } /** * 驗(yàn)證區(qū)域范圍 * @param array $coordArray * @return bool */ public static function isCityCenter(array $coordArray){ if(!self::vaildatePoint($coordArray)){ return false; } return self::pnpoly(count(self::$coordArray), $coordArray['lng'], $coordArray['lat']); } /** * 比較區(qū)域坐標(biāo) * @param $nvert * @param $testx * @param $testy * @return bool */ private static function pnpoly($nvert,$testx, $testy) { $c = false; for ($i = 0, $j = $nvert-1; $i < $nvert; $j = $i++) { if ( ( (self::$verty[$i]>$testy) != (self::$verty[$j]>$testy) ) && ($testx < (self::$vertx[$j]-self::$vertx[$i]) * ($testy-self::$verty[$i]) / (self::$verty[$j]-self::$verty[$i]) + self::$vertx[$i]) ) $c = !$c; } return $c; } /** * 驗(yàn)證坐標(biāo) * @param array $pointArray * @return bool */ private static function vaildatePoint(array $pointArray){ $maxY = $maxX = 0; $minY = $minX = 9999; foreach (self::$coordArray as $item){ if($item['lng']>$maxX) $maxX = $item['lng']; if($item['lng'] < $minX) $minX = $item['lng']; if($item['lat']>$maxY) $maxY = $item['lat']; if($item['lat'] < $minY) $minY = $item['lat']; self::$vertx[] = $item['lng']; self::$verty[] = $item['lat']; } if ($pointArray['lng'] < $minX || $pointArray['lng'] > $maxX || $pointArray['lat'] < $minY || $pointArray['lat'] > $maxY) { return false; } return true; } } /**************************** test *************************************/ $map = [ //上海 ["lng" => 121.488286, "lat" => 31.420147], ["lng" => 121.702154, "lat" => 31.294828], ["lng" => 121.780918, "lat" => 31.141157], ["lng" => 121.782068, "lat" => 30.941157], ["lng" => 121.492885, "lat" => 30.909931], ["lng" => 121.22325, "lat" => 30.890099], ["lng" => 121.161482, "lat" => 31.015526], ["lng" => 121.076395, "lat" => 31.226239], ["lng" => 121.189873, "lat" => 31.339688], ["lng" => 121.459509, "lat" => 31.41368], ]; $array = ["lat"=>31.218681,"lng"=>121.08604];//進(jìn)行驗(yàn)證的區(qū)域 validationMap::setCoordArray($map); var_dump(validationMap::isCityCenter($array));
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

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
Agnes Tachyon Build Guide | A Pretty Derby Musume
2 weeks ago
By Jack chen
Oguri Cap Build Guide | A Pretty Derby Musume
2 weeks ago
By Jack chen
Dune: Awakening - Advanced Planetologist Quest Walkthrough
4 weeks ago
By Jack chen
Date Everything: Dirk And Harper Relationship Guide
4 weeks ago
By Jack chen
Palia: Rasquellywag's Riches Quest Walkthrough
4 weeks ago
By DDD

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
