MySQL spatial query is not very suitable for map coordinates, if using: http://www.cnblogs.com/tyjsjl/p/8760002.html way to map point coordinate query is not good use, so directly using PHP to map the extent of the query more convenient point. The code is as follows:
1 /**2 * Verify that the coordinate point is within an area3 * @author Xiaoliang <[email protected]>4 * Class Validationmap5 */6 classvalidationmap{7 Private $coordArray;8 Private $vertx= [];9 Private $verty= [];Ten /** One * Set the coordinate area A * @param mixed $coordArray - */ - Public functionSetcoordarray (Array $coordArray) the { - $this->coordarray =$coordArray; - } - /** + * Verify area range - * @param array $coordArray + * @return BOOL A */ at Public functionIscitycenter (Array $coordArray){ - if(!$this->vaildatepoint ($coordArray)){ - return false; - } - return $this->pnpoly (Count($this->coordarray),$coordArray[' LNG '],$coordArray[' Lat ']); - } in /** - * Compare Regional coordinates to * @param $nvert + * @param $testx - * @param $testy the * @return BOOL * */ $ Private functionPnpoly ($nvert,$testx,$testy)Panax Notoginseng { - $c=false; the for($i= 0,$j=$nvert-1;$i<$nvert;$j=$i++) { + if( ( ($this->verty[$i]>$testy) != ($this->verty[$j]>$testy)) && ($testx< ($this->vertx[$j]-$this->vertx[$i]) * ($testy-$this->verty[$i]) / ($this->verty[$j]-$this->verty[$i]) +$this->vertx[$i]) ) A $c= !$c; the } + return $c; - } $ /** $ * Verify coordinates - * @param array $pointArray - * @return BOOL the */ - Private functionVaildatepoint (Array $pointArray){Wuyi $maxY=$maxX= 0; the $minY=$minX= 9999; - foreach($this->coordarray as $item){ Wu if($item[' LNG ']>$maxX)$maxX=$item[' LNG ']; - if($item[' LNG '] <$minX)$minX=$item[' LNG ']; About if($item[' Lat ']>$maxY)$maxY=$item[' Lat ']; $ if($item[' Lat '] <$minY)$minY=$item[' Lat ']; - $this->vertx[] =$item[' LNG ']; - $this->verty[] =$item[' Lat ']; - } A if($pointArray[' LNG '] <$minX||$pointArray[' LNG '] >$maxX||$pointArray[' Lat '] <$minY||$pointArray[' Lat '] >$maxY) { + return false; the } - return true; $ } the}
/**************************** Test *************************************
$map = [ //Shanghai
["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];//to verify the area
Validationmap::setcoordarray ($MAP);
Var_dump (Validationmap::iscitycenter ($array));
Because the map point coordinates for each region need to be queried from the database fetch, the static class is inconvenient, and the code for this article does not use static classes. The pros and cons of static classes are not described here.
Original code: https://www.cnblogs.com/liang94/p/5936583.html
PHP verifies that map coordinates are in a piece of coordinate area