I am trying to find a way of checking if a pair of lat/lng coordinates are within an area (generated by other lat/lng coordinates).
For example, if my area is a rectangle generated with these coordinates:
43.672162 , -79.43585
43.629845 , -79.314585
And I wanted to check if these coordinates are within that area:
43.651989 , -79.371993
I have tried using this package but I can't make it work: github.com/kellydunn/golang-geo
p1 := geo.NewPoint(coords[0].LatX, coords[0].LonX)
p2 := geo.NewPoint(coords[0].LatY, coords[0].LonY)
geo.NewPolygon(p1, p2)
I was wondering if anyone has an implementation of this they can share, or any resources that can point me in the right direction? I am open to using google maps API as well.
In your example, which is a rectangle, you can calculate it like this:
MinimumPoint.X = Min(p1.X, p2.X)
andMinimumPoint.Y = Min(p1.Y, p2.Y)
MaximumPoint.X = Max(p1.X, p2.X)
andMaximumPoint.Y = Max(p1.Y, p2.Y)
CheckPoint.X >= MinimumPoint.X
andCheckPoint.X <= MaximumPoint.X
andCheckPoint.Y >= MinimumPoint.Y
andCheckPoint.Y <= MaximumPoint.Y
Or you can use contains function from this: https://pkg.go.dev/github.com/paulmach/orb
Add to project:
go get github.com/paulmach/orb
This is the sample code I wrote for your question:
result: