Know if I’m in a region by latitude and longitude

Asked

Viewed 551 times

2

I need to know through coordinates if I’m in a region area, in this case, the code below is in PHP with polygon, but it’s not working. Am I missing the question of co-ordination (x, y)?

I searched something with the Google Maps API, but so far nothing.

<?php
$vertices_x = array(-4.0680,-4.0352,-4.1180,-4.0708,); // x-coordinates of the vertices of the polygon
$vertices_y = array( -63.1391,-63.0330,-63.1065, -63.0087); // y-coordinates of the vertices of the polygon
$points_polygon = count($vertices_x); // number vertices
//$longitude_x = $_GET["longitude"]; // x-coordinate of the point to test
//$latitude_y = $_GET["latitude"]; // y-coordinate of the point to test
//// For testing.  This point lies inside the test polygon.
 $longitude_x = 4.0756;
 $latitude_y =  -63.0753;

if (is_in_polygon($points_polygon, $vertices_x, $vertices_y, $longitude_x, $latitude_y)){
  echo "estou na area";
}
else echo "nao estou na area";


function is_in_polygon($points_polygon, $vertices_x, $vertices_y, $longitude_x, $latitude_y)
{
  $i = $j = $c = 0;
  for ($i = 0, $j = $points_polygon-1 ; $i < $points_polygon; $j = $i++) {
    if ( (($vertices_y[$i] > $latitude_y != ($vertices_y[$j] > $latitude_y)) &&
    ($longitude_x < ($vertices_x[$j] - $vertices_x[$i]) * ($latitude_y - $vertices_y[$i]) / ($vertices_y[$j] - $vertices_y[$i]) + $vertices_x[$i]) ) ) 
        $c = !$c;
  }
  return $c;
}
?>
  • Something very similar http://www.princiweb.com.br/blog/programacao/google-apis/google-maps-polygon.html but it also didn’t work

  • https://developers.google.com/maps/documentation/geocoding/start#ReverseGeocoding

  • I tried to open but it showed nothing

  • I figured I’d put it in my code

  • It worked now only this giving an error here $c = ! $c;

  • Where has // $i = $j = $c = 0; puts $c = 0;

  • Perfect Thank you very much !

  • https://stackoverflow.com/questions/14750275/haversine-formula-with-php

Show 3 more comments

1 answer

1


You can simplify the function by just checking the coordinates $longitude_x and $latitude_y are inside the polygon with the vertices informed in the arrays:

function is_in_polygon($points_polygon, $vertices_x, $vertices_y, $longitude_x, $latitude_y)
{
   $c = 0;
  for ($i = 0, $j = $points_polygon-1 ; $i < $points_polygon; $j = $i++) {
   if(
      $latitude_y <= $vertices_y[$i] && $latitude_y >= $vertices_y[$j] &&
      $longitude_x >= $vertices_x[$j] && $longitude_x <= $vertices_x[$i]
   )
   $c = !$c;
  }
  return $c;
}

The function checks if the Y coordinate is equal to or equal to than the first 3 vertices and greater or equal that the latter, and the same with coordinate X. So that the point given by the coordinates X,Y (lng and lat, respectively) is within the polygon area, the 4 conditions of the if must be true.

  • I did not understand in the case I will put only a coordinate that ?

  • No. The rest of the code doesn’t change, just change the function.

  • I got it perfect !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.