检查地理位置是否在边界内 [英] Check if geolocation is within boundaries

查看:100
本文介绍了检查地理位置是否在边界内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个区域的边界内生成一些地理定位,现在我试图在这些边界内生成地理位置

  $ bounds = array(
array(55.4024447,13.1656691),
array(56.1575776,15.8350261),
array(68.0410163,17.4600359),
array(58.9380148, 11.3501468),
数组(67.6820048,16.1964251)
);

现在我的检查如下所示:

  if(
($ bounds [0] [0] <$ lat& $ amp; $ bounds [0] [1] <$ lng)& amp ;&
($ bounds [1] [0]< $ lat&& $ amp; $ bounds [1] [1]> $ lng)&&
($ bounds [2 ] $ lat&& $ bounds [2] [1]> $ lng)&&
($ bounds [3] [0]> $ lat&& $ bounds [3] [1]< $ lng)&&
($ bounds [4] [0]> $ lat&& $ bounds [4] [1]> $ lng )
){

然而,这只能得到位于中间的正方形内的位置,而不在整个区域
您是否有任何关于如何在整个区域内进行检查的想法?解析方案

下面的PHP代码使用 多边形点 算法。



在图1中,点是多边形的,并且从中画出的线与p相交多边形的极坐标奇数次(3)。在图2中,该直线横过周长偶数次(4),并且该点在外侧。



<$ p $ ($ polySides,$ polyX,$ polyY,$ x,$ y){
$ j = $ polySides-1($ poly $ s





$ b $ ;
$ oddNodes = 0;如果($ polyY [$ i]< $ y&& $ polyY [$ j]> =($ i $ 0; $ i <$ polySides; $ i ++) $ y || $ polyY [$ j]< $ y&& $ polyY [$ i]> = $ y){
if($ polyX [$ i] +($ y- $ polyY [$ i])/($ polyY [$ j] - $ polyY [$ i])*($ polyX [$ j] - $ polyX [$ i])< $ x){
$ oddNodes = !$ oddNodes;
}
}
$ j = $ i; }

返回$ oddNodes;
}
$ polySides = 3;
$ x = 60;
$ y = 15;
$ polyX = array(55.4024447,56.1575776,68.0410163,67.6820048,58.9380148,55.4024447); //第一个点重复以关闭多边形坐标
$ polyY = array(13.1656691,15.8350261,17.4600359,16.1964251,11.3501468, 13.1656691);
if(pointInPolygon($ polySides,$ polyX,$ polyY,$ x,$ y)){
echoPoint In Polygon;
} else {
echoPoint Not In Polygon;
}
?>


I'm trying to generate some geolocations within the borders of an area right now I'm trying to generate geo locations within these boundaries

$bounds = array(
    array(55.4024447, 13.1656691),
    array(56.1575776, 15.8350261),
    array(68.0410163, 17.4600359),
    array(58.9380148, 11.3501468),
    array(67.6820048, 16.1964251)
);

and right now my checking for this is as follows

if (
    ($bounds[0][0] < $lat && $bounds[0][1] < $lng) &&
    ($bounds[1][0] < $lat && $bounds[1][1] > $lng) &&
    ($bounds[2][0] > $lat && $bounds[2][1] > $lng) &&
    ($bounds[3][0] > $lat && $bounds[3][1] < $lng) &&
    ($bounds[4][0] > $lat && $bounds[4][1] > $lng)
) {

however this only gets locations within a square in the middle not in the entire area do you have any idea of how to check within the full area instead?

解决方案

The following PHP code uses point in polygon algorithm.

In Fig 1 point is in polygon and the line drawn from this crosses the perimeter of the polygon an odd number of times(3). In Fig 2 the line crosses the perimeter an even number of times(4) and the point is outside.

<?php

function pointInPolygon($polySides,$polyX,$polyY,$x,$y) {
  $j = $polySides-1 ;
  $oddNodes = 0;
  for ($i=0; $i<$polySides; $i++) {
    if ($polyY[$i]<$y && $polyY[$j]>=$y  ||  $polyY[$j]<$y && $polyY[$i]>=$y) {
        if ($polyX[$i]+($y-$polyY[$i])/($polyY[$j]-$polyY[$i])*($polyX[$j]-$polyX[$i])<$x)  {
            $oddNodes=!$oddNodes; 
        }
    }
   $j=$i; }

  return $oddNodes;
}
$polySides = 3;
$x =60;
$y =15;
$polyX = array(55.4024447,56.1575776,68.0410163,67.6820048,58.9380148,55.4024447);// First point repeated to close polygon coordinates 
$polyY = array(13.1656691,15.8350261,17.4600359,16.1964251,11.3501468,13.1656691); 
if (pointInPolygon($polySides,$polyX,$polyY,$x,$y)){
    echo "Point In Polygon";
    }else{
    echo "Point Not In Polygon";
    }
?>

这篇关于检查地理位置是否在边界内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆