数组(多边形)
[[-24.837452, -65.452484],[-24.837433, -65.450645],[-24.828865000000004, -65.449997],[-24.828709, -65.451607],[-24.837452, -65.452484]]
字符串
点-24.8330000,-65.452484
如何检测点是否在多边形内?
如果我使用数组的例子作品。如果你使用真实的坐标它不工作.
<?php
class pointLocation {
var $pointOnVertex = true; // Checar si el punto se encuentra exactamente en uno de los vértices?
function pointLocation() {
}
function pointInPolygon($point, $polygon, $pointOnVertex = true) {
$this->pointOnVertex = $pointOnVertex;
// Transformar la cadena de coordenadas en matrices con valores "x" e "y"
$point = $this->pointStringToCoordinates($point);
$vertices = array();
foreach ($polygon as $vertex) {
$vertices[] = $this->pointStringToCoordinates($vertex);
}
// Checar si el punto se encuentra exactamente en un vértice
if ($this->pointOnVertex == true and $this->pointOnVertex($point, $vertices) == true) {
return "vertex";
}
// Checar si el punto está adentro del poligono o en el borde
$intersections = 0;
$vertices_count = count($vertices);
for ($i=1; $i < $vertices_count; $i++) {
$vertex1 = $vertices[$i-1];
$vertex2 = $vertices[$i];
if ($vertex1['y'] == $vertex2['y'] and $vertex1['y'] == $point['y'] and $point['x'] > min($vertex1['x'], $vertex2['x']) and $point['x'] < max($vertex1['x'], $vertex2['x'])) { // Checar si el punto está en un segmento horizontal
return "boundary";
}
if ($point['y'] > min($vertex1['y'], $vertex2['y']) and $point['y'] <= max($vertex1['y'], $vertex2['y']) and $point['x'] <= max($vertex1['x'], $vertex2['x']) and $vertex1['y'] != $vertex2['y']) {
$xinters = ($point['y'] - $vertex1['y']) * ($vertex2['x'] - $vertex1['x']) / ($vertex2['y'] - $vertex1['y']) + $vertex1['x'];
if ($xinters == $point['x']) { // Checar si el punto está en un segmento (otro que horizontal)
return "boundary";
}
if ($vertex1['x'] == $vertex2['x'] || $point['x'] <= $xinters) {
$intersections++;
}
}
}
// Si el número de intersecciones es impar, el punto está dentro del poligono.
if ($intersections % 2 != 0) {
return "inside";
} else {
return "outside";
}
}
function pointOnVertex($point, $vertices) {
foreach($vertices as $vertex) {
if ($point == $vertex) {
return true;
}
}
}
function pointStringToCoordinates($pointString) {
$coordinates = explode(" ", $pointString);
return array("x" => $coordinates[0], "y" => $coordinates[1]);
}
}
//original examples is OK
//$points = array("50 70","70 40","-20 30","100 10","-10 -10","40 -20","110 -20");
//$polygon = array("-50 30","50 70","100 50","80 10","110 -10","110 -30","-20 -50","-30 -40","10 -10","-10 10","-30 -20","-50 30");
$json="[[-24.837452, -65.452484],[-24.837433, -65.450645],[-24.828865000000004, -65.449997],[-24.828709, -65.451607],[-24.837452, -65.452484]]";
$resultado = str_replace(",", "", $json);
$resultado = str_replace("[[", "", $resultado);
$resultado = str_replace("]]", "", $resultado);
$polygon=explode("][", $resultado);
$points=array("-24.8330000 -65.452484");
//print_r ($resultado);
$pointLocation = new pointLocation();
// Las últimas coordenadas tienen que ser las mismas que las primeras, para "cerrar el círculo"
foreach($points as $key => $point) {
echo "point " . ($key+1) . " ($point): " . $pointLocation->pointInPolygon($point, $polygon) . "<br>";
}
?>
型
这些数据是真实的,并且点位于面内
[[-24.837452,-65.452484],[-24.837433,-65.450645],[-24.82886500000004,-65.449997],[-24.828709,-65.451607],[-24.837452,-65.452484]]
点-24.8330000,-65.452484
6条答案
按热度按时间klh5stk11#
字符串
$fenceArea => array of multiple lat lng $fenceArea=json_decode('[{"lat":15.20184549675828,"lng":73.97872613764991},{"lat":15.10939000042012,"lng":73.93821405268898},{"lat":15.035131321967592,"lng":74.00619195796241},{"lat":14.991027988389707,"lng":74.05288385249366},{"lat":14.97268353612274,"lng":74.10815881587257},{"lat":15.08885107597101,"lng":74.17785333491554},{"lat":15.179658827735558,"lng":74.16652368403663},{"lat":15.245254746972826,"lng":74.18746637202491},{"lat":15.31261799705796,"lng":74.1534774193882},{"lat":15.35260926883264,"lng":74.06195113314038},{"lat":15.396694714469158,"lng":73.96676107157123},{"lat":15.480490697370463,"lng":73.91443209172701},{"lat":15.485848509162427,"lng":73.84096243101556},{"lat":15.58045246934704,"lng":73.83169271666009},{"lat":15.596325797024097,"lng":73.75410177427727},{"lat":15.406706231349043,"lng":73.79266521010072}]'); $point => Searching particular lat lng $point=["lat": 15.381533561369851, "lng": 73.90972848645542];
如果点在fenceArea内,则返回true,否则返回false。
hujrc8aj2#
如果你只是想使用谷歌Map检查多边形是否包含你的点,你可以尝试:谷歌MapAPI
如果你想在PHP中计算它,那么你的问题是这个问题的重复:Check if Google Map Point is in polygon from PHP
rm5edbpk3#
这里的另一个答案可能是你最好的选择,但如果它不工作的任何原因,它看起来像你要尝试的Ray-casting algorithm.的一个版本,一些其他用户已经尝试过,你可以看看here作为参考。祝你好运!
oyxsuwqo4#
以下是您使用Google Maps API时遇到的问题的工作实现(因为您已标记了它):jsFiddle
google.maps.geometry.poly.containsLocation(testPointCoords, polygonObject)
如果点在多边形内,则返回true;如果点在多边形外,则返回false。关于几何库的更多信息在这里:Google Maps Geometry Library
containsLocation()函数:contains Location()
qltillow5#
已经解决了更多的空间坐标
字符串
pdkcd3nj6#
给了拉万尼亚E.的答案几个调整,因为它不是为我运行。
由于我的应用程序需要针对多个多边形(围栏区域)检查多个点,我还调整了JSON以处理嵌套数组。
字符串