php - Check if geolocation is within boundaries -
i'm trying generate geolocations within borders of area right i'm trying generate geo locations within these boundaries
$bounds = array( array(55.4024447, 13.1656691), array(56.1575776, 15.8350261), array(68.0410163, 17.4600359), array(58.9380148, 11.3501468), array(67.6820048, 16.1964251) );
and right checking follows
if ( ($bounds[0][0] < $lat && $bounds[0][1] < $lng) && ($bounds[1][0] < $lat && $bounds[1][1] > $lng) && ($bounds[2][0] > $lat && $bounds[2][1] > $lng) && ($bounds[3][0] > $lat && $bounds[3][1] < $lng) && ($bounds[4][0] > $lat && $bounds[4][1] > $lng) ) {
however gets locations within square in middle not in entire area have idea of how check within full area instead?
the following php code uses point in polygon algorithm.
in fig 1 point in polygon , line drawn crosses perimeter of polygon odd number of times(3). in fig 2 line crosses perimeter number of times(4) , point outside.
<?php function pointinpolygon($polysides,$polyx,$polyy,$x,$y) { $j = $polysides-1 ; $oddnodes = 0; ($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; } $polysides = 3; $x =60; $y =15; $polyx = array(55.4024447,56.1575776,68.0410163,67.6820048,58.9380148,55.4024447);// first point repeated close polygon coordinates $polyy = array(13.1656691,15.8350261,17.4600359,16.1964251,11.3501468,13.1656691); if (pointinpolygon($polysides,$polyx,$polyy,$x,$y)){ echo "point in polygon"; }else{ echo "point not in polygon"; } ?>
Comments
Post a Comment