沃梦达 / 编程问答 / php问题 / 正文

谷歌地图:是多边形内的纬度/经度吗?

Google Map: is a lat/lng within a polygon?(谷歌地图:是多边形内的纬度/经度吗?)

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

问题描述

给定一对 lat/lng 值,我如何确定该对是否在多边形内?我需要在 PHP 中执行此操作.我看到 Google Maps API 有一个 containsLocation 方法:https://developers.google.com/maps/documentation/javascript/reference.有没有办法从 PHP 中利用它?

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.

我已经在 php 中实现了这篇 Point in Polygon 文章中的 C 代码并使用下面的多边形来说明.

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";
?>

这篇关于谷歌地图:是多边形内的纬度/经度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:谷歌地图:是多边形内的纬度/经度吗?

基础教程推荐