Madhav Palshikar
Madhav Palshikar

Reputation: 312

How to find out the given latitude and longitude are coming under specified area?

I am using this code http://gmaps-utility-library-dev.googlecode.com/svn/trunk/geometrycontrols/examples/test.html for the selection of the area on google map. After selection I am getting all the list of latitudes and longitudes of the selected area. The problem is I have latitude and longitude of one point, I want to find out that point is coming under the selected area or not using PHP.

Thank You guyz for your reply. I got the perfect answer here Find Point in polygon PHP

Upvotes: 1

Views: 929

Answers (1)

cihanduruer
cihanduruer

Reputation: 135

If i am not misunderstood, you are trying to find selected point (lat, lon) is in area of 4 other point (lat,lon) sort of like this case;

  Ax,Ay              Bx,By
A(10,20)           B(20,20)
+-----------------+
|         Px,Py   |
|        (15,15)  |
|       P         |
|                 |
|                 |
+-----------------+
  Cx,Cy              Dx,Dy
C(10,10)           D(20,10)

you can find this information matematically A, B, C, D is the corner points of rectangle and P is a single point what you looking for. Here is the code piece which can help you to find P is in square or not.

PS: Code is not written in editor, so it may contain typo or error. Please check carefully.

function isInsideSquare($Ax, $Ay, $Bx, $By, $Cx, $Cy, $Px, $Py) {
    if (triangleArea($Ax,$Ay,$Bx,$By,$Px,$Py)>0 || triangleArea($Bx,$By,$Cx,$Cy,$Px,$Py)>0 || triangleArea($Cx,$Cy,$Dx,$Dy,$Px,$Py)>0 || triangleArea($Dx,$Dy,$Ax,$Ay,$Px,$Py)>0) {
        return false;
    }
    return true;
}


function triangleArea($Ax, $Ay, $Bx, $By, $Cx, $Cy){
    return ($Cx*$By-$Bx*$Cy)-($Cx*$Ay-$Ax*$Cy)+($Bx*$Ay-$Ax*$By);
}

Upvotes: 3

Related Questions