判断一个点是否在某个区域内。百度,高德,腾讯都能用。(php版)

判断一个点是否在某个区域内。百度,高德,腾讯都能用。(php版)

<?php
// *** 配置文件(表示区域的三维数组)其内的点,必须按顺时针方向依次给出!
$area = array(
// 天通苑店
0 => array(
array('x'=>116.38295, 'y'=>40.09416),
array('x'=>116.44037, 'y'=>40.095898),
array('x'=>116.448275,'y'=>40.083313),
array('x'=>116.448455,'y'=>40.050818),
array('x'=>116.448275,'y'=>40.038307),
array('x'=>116.441448,'y'=>40.038418),
array('x'=>116.436058,'y'=>40.038804),
array('x'=>116.417302,'y'=>40.039136),
array('x'=>116.414822,'y'=>40.039384),
array('x'=>116.412738,'y'=>40.039329),
array('x'=>116.407672,'y'=>40.039329),
array('x'=>116.388628,'y'=>40.085162),
array('x'=>116.383633,'y'=>40.084997)
),
//亚运村
1 => array(
array('x'=>116.358804,'y'=>40.028474),
array('x'=>116.41608, 'y'=>40.02875),
array('x'=>116.41723, 'y'=>40.038915),
array('x'=>116.447988,'y'=>40.037921),
array('x'=>116.447844,'y'=>40.026761),
array('x'=>116.455821,'y'=>40.024164),
array('x'=>116.446281,'y'=>39.994736),
array('x'=>116.443532,'y'=>39.995372),
array('x'=>116.376267,'y'=>39.993493),
array('x'=>116.375908,'y'=>40.000015),
array('x'=>116.372027,'y'=>39.999904),
array('x'=>116.371452,'y'=>40.007366),
array('x'=>116.359451,'y'=>40.006758)
),
//望京店
2 => array(
array('x'=>116.46387, 'y'=>40.021125),
array('x'=>116.484495,'y'=>40.020462),
array('x'=>116.515684,'y'=>39.995151),
array('x'=>116.51519, 'y'=>39.976137),
array('x'=>116.491906,'y'=>39.972985),
array('x'=>116.476239,'y'=>39.977298),
array('x'=>116.467472,'y'=>39.96917),
array('x'=>116.443325,'y'=>39.984817),
array('x'=>116.449506,'y'=>39.993109),
array('x'=>116.446357,'y'=>39.994736),
array('x'=>116.456037,'y'=>40.024109)
),
//大悦城店
3 => array(
array('x'=>116.496424,'y'=>39.96253),
array('x'=>116.479527,'y'=>39.975491),
array('x'=>116.492921,'y'=>39.972491),
array('x'=>116.508533,'y'=>39.974454),
array('x'=>116.535231,'y'=>39.980225),
array('x'=>116.553485,'y'=>39.975691),
array('x'=>116.564624,'y'=>39.975028),
array('x'=>116.571307,'y'=>39.972097),
array('x'=>116.571666,'y'=>39.946989),
array('x'=>116.547736,'y'=>39.947763),
array('x'=>116.549245,'y'=>39.936755),
array('x'=>116.564624,'y'=>39.937142),
array('x'=>116.569367,'y'=>39.92995),
array('x'=>116.570085,'y'=>39.915175),
array('x'=>116.496424,'y'=>39.914843)
),
//北洼路
4 => array(
array('x'=>116.24763, 'y'=>39.978677),
array('x'=>116.280975,'y'=>39.976244),
array('x'=>116.322872,'y'=>39.991226),
array('x'=>116.323231,'y'=>39.986859),
array('x'=>116.339975,'y'=>39.986859),
array('x'=>116.340263,'y'=>39.982215),
array('x'=>116.346443,'y'=>39.98216),
array('x'=>116.354492,'y'=>39.963415),
array('x'=>116.361822,'y'=>39.963637),
array('x'=>116.362397,'y'=>39.957664),
array('x'=>116.37792, 'y'=>39.958106),
array('x'=>116.37138, 'y'=>39.929728),
array('x'=>116.367068,'y'=>39.929341),
array('x'=>116.366637,'y'=>39.924361),
array('x'=>116.37138, 'y'=>39.924361),
array('x'=>116.369655,'y'=>39.913626),
array('x'=>116.362325,'y'=>39.912962),
array('x'=>116.363188,'y'=>39.903774),
array('x'=>116.317194,'y'=>39.902999),
array('x'=>116.259415,'y'=>39.902778)
),
//安贞店
5 => array(
array('x'=>116.372171,'y'=>39.993161),
array('x'=>116.443676,'y'=>39.994985),
array('x'=>116.448419,'y'=>39.993548),
array('x'=>116.43807, 'y'=>39.97735),
array('x'=>116.438789,'y'=>39.9656),
array('x'=>116.440298,'y'=>39.955839),
array('x'=>116.42262, 'y'=>39.955673),
array('x'=>116.378531,'y'=>39.954815),
array('x'=>116.377704,'y'=>39.963941),
array('x'=>116.368003,'y'=>39.96383),
array('x'=>116.367679,'y'=>39.973341),
array('x'=>116.361247,'y'=>39.973479),
array('x'=>116.360529,'y'=>39.987025),
array('x'=>116.37235, 'y'=>39.987716)  
),
//三元桥
6 => array(
array('x'=>116.283706,'y'=>40.114137  ),    
array('x'=>116.354995,'y'=>40.121613  ),    
array('x'=>116.369081,'y'=>40.114661  ),    
array('x'=>116.37871, 'y'=>40.114772  ),   
array('x'=>116.380435,'y'=>40.106826  ),    
array('x'=>116.385897,'y'=>40.107543  ),    
array('x'=>116.389346,'y'=>40.07782   ),   
array('x'=>116.370949,'y'=>40.075998  ),    
array('x'=>116.37174, 'y'=>40.05739   ),  
array('x'=>116.325746,'y'=>40.042643  )  
),
//团结湖
7 => array(
array('x'=>116.43189, 'y'=>39.955341),
array('x'=>116.440316,'y'=>39.955396),
array('x'=>116.438394,'y'=>39.977225),
array('x'=>116.442849,'y'=>39.984116),
array('x'=>116.467876,'y'=>39.969194),
array('x'=>116.476608,'y'=>39.976797),
array('x'=>116.494969,'y'=>39.963637),
array('x'=>116.496999,'y'=>39.914179),
array('x'=>116.433902,'y'=>39.914511)
),
//劲松店
8 => array(
array('x'=>116.4053,  'y'=>39.90632  ),
array('x'=>116.495418,'y'=>39.911412 ),   
array('x'=>116.495418,'y'=>39.87709  ),  
array('x'=>116.491322,'y'=>39.854717 ),   
array('x'=>116.462432,'y'=>39.851892 ),   
array('x'=>116.421362,'y'=>39.852141 ),   
array('x'=>116.420895,'y'=>39.863412 ),   
array('x'=>116.406809,'y'=>39.863412 ),   
array('x'=>116.406881,'y'=>39.863357 )  
),
//黄村店
9 => array(
array('x'=>116.280184,'y'=>39.776406 ),  
array('x'=>116.314751,'y'=>39.788383 ),  
array('x'=>116.337586,'y'=>39.805041 ),  
array('x'=>116.353252,'y'=>39.804487 ),  
array('x'=>116.356001,'y'=>39.794703 ),  
array('x'=>116.364912,'y'=>39.799138 ),  
array('x'=>116.367787,'y'=>39.783836 ),  
array('x'=>116.378495,'y'=>39.781507 ),  
array('x'=>116.383094,'y'=>39.766479 ),  
array('x'=>116.388628,'y'=>39.76426  ), 
array('x'=>116.387478,'y'=>39.749616 ),  
array('x'=>116.383238,'y'=>39.748507 ),  
array('x'=>116.385178,'y'=>39.728255 ),  
array('x'=>116.352408,'y'=>39.727423 ),  
array('x'=>116.335592,'y'=>39.705888 ),  
array('x'=>116.301726,'y'=>39.697727 )  
)
);
/*
*** 配置文件(表示区域的三维数组)其内的点,必须按顺时针方向依次给出!
*** 确定一点是否在一区域(多边形)内:
1:过这一点(x0, y0),画一水平线(y=y0),与多边形的所有边进行交点判断。
2:获取交点集(其中不含多边形的顶点)
3:若该点(x0, y0)的左侧和右侧交点个数均为奇数个,则该点在区域(多边形)内。否则:不在。
*** 返回结果:
return === false : 点不在区域内
return 0, 1, 2, 3 ... 点所在的区域编号(配置文件中的区域编号。)
*** Author : Guojunzhou / Eric
*** Main : php20141104@163.com	
*/
class Area{
// 一个表示区域的三维数组
protected $config = null;
// 包含每个区域的四边形
protected $rectangles = null;
// 每个区域(多边形)的所有边
protected $lines = null;
// 要判断的点的x, y坐标
protected $_x = null;
protected $_y = null;
public function __construct($config){
$this->config = $config;
$this->initRectangles();
$this->initLines();
}
/*
获取包含每个配送区域的四边形
*/
private function initRectangles(){
foreach ($this->config as $k => $v) {
$this->rectangles[$k]['minX'] = $this->getMinXInEachConfig($k);
$this->rectangles[$k]['minY'] = $this->getMinYInEachConfig($k);
$this->rectangles[$k]['maxX'] = $this->getMaxXInEachConfig($k);
$this->rectangles[$k]['maxY'] = $this->getMaxYInEachConfig($k);
}
}
/*
初始化每个区域(多边形)的边(线段:直线的一部分【限制x或者y坐标范围】)
n 个顶点构成的多边形,有 n-1 条边
*/
private function initLines(){
foreach ($this->config as $k => $v) {
$pointNum = count($v);		// 区域的顶点个数
$lineNum = $pointNum - 1; 	// 区域的边条数
for($i=0; $i<$lineNum; $i++){
// y=kx+b : k
if($this->config[$k][$i]['x'] - $this->config[$k][$i+1]['x'] == 0) $this->lines[$k][$i]['k'] = 0;
else $this->lines[$k][$i]['k'] = 
($this->config[$k][$i]['y'] - $this->config[$k][$i+1]['y'])/($this->config[$k][$i]['x'] - $this->config[$k][$i+1]['x']);
// y=kx+b : b	
$this->lines[$k][$i]['b'] = $this->config[$k][$i+1]['y'] - $this->lines[$k][$i]['k'] * $this->config[$k][$i+1]['x'];
$this->lines[$k][$i]['lx'] = min($this->config[$k][$i]['x'], $this->config[$k][$i+1]['x']);
$this->lines[$k][$i]['rx'] = max($this->config[$k][$i]['x'], $this->config[$k][$i+1]['x']);
}
$pointNum-=1;
if($this->config[$k][$pointNum]['x'] - $this->config[$k][0]['x'] == 0) $this->lines[$k][$pointNum]['k'] = 0;
else $this->lines[$k][$pointNum]['k'] =
($this->config[$k][$pointNum]['y'] - $this->config[$k][0]['y'])/($this->config[$k][$pointNum]['x'] - $this->config[$k][0]['x']);
// y=kx+b : b
$this->lines[$k][$pointNum]['b'] = $this->config[$k][0]['y'] - $this->lines[$k][$pointNum]['k'] * $this->config[$k][0]['x'];
$this->lines[$k][$pointNum]['lx'] = min($this->config[$k][$pointNum]['x'], $this->config[$k][0]['x']);
$this->lines[$k][$pointNum]['rx'] = max($this->config[$k][$pointNum]['x'], $this->config[$k][0]['x']);
}
}
/*
获取一组坐标中,x坐标最小值
*/
private function getMinXInEachConfig($index){
$minX = 200;
foreach ($this->config[$index] as $k => $v) {
if($v['x'] < $minX){
$minX = $v['x'];
}
}
return $minX;
}
/*
获取一组坐标中,y坐标最小值
*/
private function getMinYInEachConfig($index){
$minY = 200;
foreach ($this->config[$index] as $k => $v) {
if($v['y'] < $minY){
$minY = $v['y'];
}
}
return $minY;
}
/*
获取一组坐标中,x坐标最大值
*/
public function getMaxXInEachConfig($index){
$maxX = 0;
foreach ($this->config[$index] as $k => $v) {
if($v['x'] > $maxX){
$maxX = $v['x'];
}
}
return $maxX;
}
/*
获取一组坐标中,y坐标最大值
*/
public function getMaxYInEachConfig($index){
$maxY = 0;
foreach ($this->config[$index] as $k => $v) {
if($v['y'] > $maxY){
$maxY = $v['y'];
}
}
return $maxY;
}
/*
获取 y=y0 与特定区域的所有边的交点,并去除和顶点重复的,再将交点分为左和右两部分
*/	
private function getCrossPointInCertainConfig($index){
$crossPoint = null;
foreach ($this->lines[$index] as $k => $v) {
if($v['k'] == 0) return true;
$x0 = ($this->_y - $v['b']) / $v['k'];	// 交点x坐标
if($x0 == $this->_x) return true;		// 点在边上
if($x0 > $v['lx'] && $x0 < $v['rx']){
if($x0 < $this->_x) $crossPoint['left'][] = $x0;
if($x0 > $this->_x) $crossPoint['right'][] = $x0;
}
}
return $crossPoint;
}
/*
检测一个点,是否在区域内
返回结果:
return === false : 点不在区域内
return 0, 1, 2, 3 ... 点所在的区域编号(配置文件中的区域编号。)
*/
public function checkPoint($x, $y){
$this->_x = $x;
$this->_y = $y;
$contain = null;
foreach ($this->rectangles as $k => $v) {
if($x > $v['maxX'] || $x < $v['minX'] || $y > $v['maxY'] || $y < $v['minY']){
continue;
}else{
$contain = $k;
break;
}
}
if($contain === null) return false;
$crossPoint = $this->getCrossPointInCertainConfig($contain);
if($crossPoint === true) return $contain;
if(count($crossPoint['left'])%2 == 1 && count($crossPoint['right'])%2 == 1) return $contain;
return false;
}
}
$area = new Area($area);
var_dump($area->checkPoint(116.531748,39.944229));
?>

  判断一个点是否在某个区域内。百度,高德,腾讯都能用。(php版)

https://blog.csdn.net/u014290054/article/details/47418065

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/112353.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • nginx负载均衡原理简介_nginx负载均衡配置详解

    nginx负载均衡原理简介_nginx负载均衡配置详解nginx负载均衡中常见的算法及原理有哪些?Nginx可以基于ngx_http_upstream_module模块提供服务器分组转发、权重分配、状态监测、调度算法等高级功能httpupstream配置参数#自定义一组服务器,配置在http块内upstreamname{server………..}#示例upstreambackend{serverbackend1.example.comweight=5;server127.0.

  • for while循环语句举例python_python中while和for循环的用法

    for while循环语句举例python_python中while和for循环的用法程序在一般情况下是按顺序执行的。编程语言提供了各种控制结构,允许更复杂的执行路径。循环语句允许我们执行一个语句或语句组多次,下面是在大多数编程语言中的循环语句的一般形式1.循环控制语句在了解循环语句的使用方法之前,我们先来了解几个循环控制语句:1)…

  • 小型企业的网络拓扑结构设计

    小型企业的网络拓扑结构设计小型企业的网络拓扑结构设计一、设计目的企业局域网的最终目标是建设整个单位的互联、统一、高效、实用、安全的局域网络,近期可支持上百个,远期至少可支持上午个并发用户,提供广泛的资源共享(包括硬件、软件和信息资源共享)。网络结构清楚、布线合理、充分考虑房间分布;局域网性能稳定、安全;软、硬件结合良好,公司日常办公需要,方便资源共享、游览有良好的兼容性和可扩展性,具备单位局域网与其他单位局域网互连,并…

  • navicat for mysql 15激活码_在线激活2022.01.18

    (navicat for mysql 15激活码)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.cn/100143.html0HKLM1UCCY-eyJsaWNlbnNlSW…

  • Web Service学习笔记

    Web Service学习笔记

  • clion2022 激活【中文破解版】「建议收藏」

    (clion2022 激活)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.cn/100143.html1TCF2R91JZ-eyJsa…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号