問題描述
給定一對(duì) lat/lng 值,我如何確定該對(duì)是否在多邊形內(nèi)?我需要在 PHP 中執(zhí)行此操作.我看到 Google Maps API 有一個(gè) 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?
推薦答案
判斷點(diǎn)是否在多邊形內(nèi)的一種方法是計(jì)算從該點(diǎn)(在任何方向)繪制的線與多邊形邊界相交的次數(shù).如果它們相交的次數(shù)為偶數(shù),則該點(diǎn)在外面.
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.
我已經(jīng)在 php 中實(shí)現(xiàn)了這篇 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";
?>
這篇關(guān)于谷歌地圖:是多邊形內(nèi)的緯度/經(jīng)度嗎?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!