Ali Raza
Ali Raza

Reputation: 55

Find points in four directions of a latitude and longitude at certain distance

I am working on showing four pointers around a location (a pointer with certain latitude and longitude), which can be selected on a map.

So i get the location's lat and long values from https://nominatim.org/ api. What i'm trying to work on is get four point's lat and long value from the selected location. These four points are simply 100 meters in distance from the current point.

So now the issue here is how do i calculate or find a point east, west, north and south of the selected location's point (100 meters from the current lat long position). i have checked different options like point in polygon algorithm here https://assemblysys.com/php-point-in-polygon-algorithm/, or trying to draw a circle around the selected point and then getting four points on that circle's circumference, however the issue is still that i'm not able to know if the point which i have got is in which direct from the selected location. Is it east of location, south or west etc.

Any help is appreciated about identifying that a certain point is in which direction of the selected point.

Upvotes: 1

Views: 765

Answers (1)

stuparm
stuparm

Reputation: 553

Once you (or someone) select the location on the map, you get its latitude and longitude. Let's say it is:

lat = 44.78
lon = 20.45

Then, take a look at this answer: Calculating new longitude, latitude from old + n meters. It should give you the distance of 100 meters, but as latitude/longitude offset. Once you apply the formula from SO answer, you would get:

lat_offset = 0.0008983152841195215
lon_offset = 0.001265559599380766      // note that this lon_offset contains lat in formula (take a look at the link above)
                                       

Finally, apply the calculated in your formula:

west_lat = lat
west_lon = lon - lon_offset  

east_lat = lat
east_lon = lon + lon_offset

north_lat = lat + lat_offset
north_lon = lon

south_lat = lat - lat_offset
south_lon = lon  

Note: These formulas does not work for positions that are very close to south or north pole

Upvotes: 1

Related Questions