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";
?>
这篇关于谷歌地图:是多边形内的纬度/经度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:谷歌地图:是多边形内的纬度/经度吗?
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- Laravel 仓库 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01