Google地图:在多边形内是纬度/经度? [英] Google Map: is a lat/lng within a polygon?
问题描述
containsLocation
方法: https: //developers.google.com/maps/documentation/javascript/reference 。有没有一种方法可以从PHP中利用它?查找点是否在多边形中的一种方法是计算很多时候从点(任何方向)绘制的线与多边形边界相交。如果它们交叉偶数次,那么这个点就在外面。
我已经从这个 指向Polygon 的文章,并使用下面的多边形进行说明。
<?php
// Point-In-Polygon Algorithm
$ polySides = 4; //多边形有多少个角
$ polyX = array(4,9,11,2); //角的水平坐标
$ polyY = array(10,7,2,2); //角的垂直坐标
$ x = 3.5;
$ y = 13.5; //
// $ y = 3.5之外// //
函数pointInPolygon($ polySides,$ polyX,$ polyY,$ x,$ y){
$ j = $ polySides-1;
$ oddNodes = 0;如果($ polyY [$ i]< $ y&& $ polyY [$ j]> =($ i $ 0; $ i <$ polySides; $ i ++) $ y
|| $ polyY [$ j]< $ y&& $ polyY [$ i]> = $ y){
if($ polyX [$ i] +($ $ $ polyY [$ i])/($ polyY [$ j] - $ polyY [$ i])*($ polyX [$ j] - $ polyX [$ i])<$ x){
$ oddNodes =!$ oddNodes; }}
$ j = $ i; }
返回$ oddNodes; }
$ b $ if(pointInPolygon($ polySides,$ polyX,$ polyY,$ x,$ y)){
echoIs in polygon!;
}
else echo不在多边形中;
?>
Given a pair of lat/lng values, how do I determine if the pair is within a polygon? I need to do this in PHP. I see that Google Maps API has a containsLocation
method: https://developers.google.com/maps/documentation/javascript/reference. Is there a way to leverage this from PHP?
One way to find if a point is in a polygon is to count how many times a line drawn from the point (in any direction) intersects with the polygon boundary. If they intersect an even number of times, then the point is outside.
I have implemented the C code from this Point in Polygon article in php and used the polygon below to illustrate.
<?php
//Point-In-Polygon Algorithm
$polySides = 4; //how many corners the polygon has
$polyX = array(4,9,11,2);//horizontal coordinates of corners
$polyY = array(10,7,2,2);//vertical coordinates of corners
$x = 3.5;
$y = 13.5;//Outside
//$y = 3.5;//Inside
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; }
if (pointInPolygon($polySides,$polyX,$polyY,$x,$y)){
echo "Is in polygon!";
}
else echo "Is not in polygon";
?>
这篇关于Google地图:在多边形内是纬度/经度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!