0

Im using javascript. But even pseudocode works to solve this issue.

So I have a place located at a certain latitude/longitude. More exactly at:

latitude: 59.33312059999999
longitude: 18.0694596

I then have a list of latitude/longitude. These coordinates are the points for a polygon. In my test, the coordinates generated for a rectangle (that contains the place above) are:

lat:59.33494869033997
lng:18.076973852002993

lat:59.328907172141186
lng:18.076973852002993

lat:59.328907172141186
lng:18.055945333326235

lat:59.33494869033997
lng:18.055945333326235

What I wonder is how the if-statement should look like to understand if the place is inside the coordinates for the drawn rectangle.
In this case it is a rectangle, but it could be any geometrical shape (not circle though).

I was thinking I could extract the lowest and highest latitude/longitude and then just have:

IF latitude for place <= highest latitude 
AND latitude for place >= lowest latitude
AND longitude for place <= highest longitude 
AND longitude for place >= lowest longitude
THEN place is inside the coordinates

Or is there a better way?

oderfla
  • 1,695
  • 4
  • 24
  • 49
  • 1
    Possible duplicate of [How can I determine whether a 2D Point is within a Polygon?](https://stackoverflow.com/questions/217578/how-can-i-determine-whether-a-2d-point-is-within-a-polygon) – pishpish Oct 30 '17 at 15:11
  • 1
    Possible duplicate of [JS- Check if Point Inside A Polygon](https://stackoverflow.com/questions/22521982/js-check-if-point-inside-a-polygon) – Indent Oct 30 '17 at 15:11
  • 1
    Your if statement should work for a rectangle, but will not generalize to any polygon – Code-Apprentice Oct 30 '17 at 15:11
  • Ok I suspected that. Any advice where to look? – oderfla Oct 30 '17 at 15:12
  • Note that for a general polygon, you must be careful. Geometry on the surface of a sphere is different from Euclidean Geometry that you learn in high school. – Code-Apprentice Oct 30 '17 at 15:13
  • 1
    Also, if you will have a self intersecting polygons it will hard to determine. – Observer Oct 30 '17 at 15:13
  • Do not reinvent the wheel. This computation already exists in Google Maps API and others. – Code-Apprentice Oct 30 '17 at 15:14
  • 1
    @oderfla this is for google map api or other api or may be your own it depends of the content size of the map using but in general is like this : `Scale_X = ( Latitude_North - Latitude_Sud) / Width_Map` and `Scale_Y = ( Longitude_Ouest - Longitude_Est) / Height_Map` –  Oct 30 '17 at 15:21

0 Answers0