Google地图:在多边形内是纬度/经度? [英] Google Map: is a lat/lng within a polygon?

查看:132
本文介绍了Google地图:在多边形内是纬度/经度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一对经纬度值,我如何确定这个对是否在一个多边形内?我需要在PHP中执行此操作。我发现Google Maps API有一个 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屋!

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