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";
?>
这篇关于谷歌地图:是多边形内的纬度/经度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:谷歌地图:是多边形内的纬度/经度吗?
基础教程推荐
- 超薄框架REST服务两次获得输出 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01