标签:cti auth mysq amp date set ret 空间查询 坐标
mysql空间查询并不太适合地图坐标,如果使用:http://www.cnblogs.com/tyjsjl/p/8760002.html的方式进行地图点坐标的查询就不好用了,于是直接使用php来进行地图范围的查询更方便点。代码如下:
1 /** 2 * 验证坐标点是否在某区域内 3 * @author xiaoliang <1058436713@qq.com> 4 * Class validationMap 5 */ 6 class validationMap{ 7 private $coordArray; 8 private $vertx = []; 9 private $verty = []; 10 /** 11 * 设置坐标区域 12 * @param mixed $coordArray 13 */ 14 public function setCoordArray(array $coordArray) 15 { 16 $this->coordArray = $coordArray; 17 } 18 /** 19 * 验证区域范围 20 * @param array $coordArray 21 * @return bool 22 */ 23 public function isCityCenter(array $coordArray){ 24 if(!$this->vaildatePoint($coordArray)){ 25 return false; 26 } 27 return $this->pnpoly(count($this->coordArray), $coordArray[‘lng‘], $coordArray[‘lat‘]); 28 } 29 /** 30 * 比较区域坐标 31 * @param $nvert 32 * @param $testx 33 * @param $testy 34 * @return bool 35 */ 36 private function pnpoly($nvert,$testx, $testy) 37 { 38 $c = false; 39 for ($i = 0, $j = $nvert-1; $i < $nvert; $j = $i++) { 40 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]) ) 41 $c = !$c; 42 } 43 return $c; 44 } 45 /** 46 * 验证坐标 47 * @param array $pointArray 48 * @return bool 49 */ 50 private function vaildatePoint(array $pointArray){ 51 $maxY = $maxX = 0; 52 $minY = $minX = 9999; 53 foreach ($this->coordArray as $item){ 54 if($item[‘lng‘]>$maxX) $maxX = $item[‘lng‘]; 55 if($item[‘lng‘] < $minX) $minX = $item[‘lng‘]; 56 if($item[‘lat‘]>$maxY) $maxY = $item[‘lat‘]; 57 if($item[‘lat‘] < $minY) $minY = $item[‘lat‘]; 58 $this->vertx[] = $item[‘lng‘]; 59 $this->verty[] = $item[‘lat‘]; 60 } 61 if ($pointArray[‘lng‘] < $minX || $pointArray[‘lng‘] > $maxX || $pointArray[‘lat‘] < $minY || $pointArray[‘lat‘] > $maxY) { 62 return false; 63 } 64 return true; 65 } 66 }
/**************************** 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];//进行验证的区域
validationMap::setCoordArray($map);
var_dump(validationMap::isCityCenter($array));
因为每个区域的地图点坐标需要从数据库提取进行查询,所以静态类就不太方便了,本文代码不使用静态类。关于静态类的优缺点在此不作说明。
原文代码:https://www.cnblogs.com/liang94/p/5936583.html
标签:cti auth mysq amp date set ret 空间查询 坐标
原文地址:https://www.cnblogs.com/tyjsjl/p/8855253.html