3

I have drawn a circle in Bing maps. Now I need to write code for whether a point (latitude, longitude) is inside or outside of the circle?

Is there any algorithmic code in c#.Net?

Pablo Claus
  • 5,886
  • 3
  • 29
  • 38
GSReddy
  • 137
  • 1
  • 5
  • 13
  • possible duplicate of [Equation for testing if a point is inside a circle](http://stackoverflow.com/questions/481144/equation-for-testing-if-a-point-is-inside-a-circle) – Shai Jul 30 '12 at 12:37
  • @Shai Not a duplicate. You don't compute distance for GPS coordinates with the sqrt(x²+y²) formula. – ken2k Jul 30 '12 at 12:43
  • my bad. [this one fits better](http://stackoverflow.com/questions/4287780/detecting-whether-a-gps-coordinate-falls-within-a-polygon-on-a-map) – Shai Jul 30 '12 at 12:54
  • Actually, if the radius of the circle is less than 20km or so then using Pythagorean doesn't have too much error. You must first convert lat/long to Cartesian coordinates. Haversine is generally faster if you don't already have Cartesian coordinates. – Peter Ritchie Jul 30 '12 at 15:08

1 Answers1

5

Just compute the distance between the center of the circle and your current coordinate, and compare this distance to the circle radius (distance <= radius means the coordinate is inside the circle).

To compute the distance between two points, use the Haversine formula.

You'll find a C# implementation here.

ken2k
  • 48,145
  • 10
  • 116
  • 176
  • In Haversine formula, in which format we have to provide longitude and latitude. (Means in decimals or degress minutes seconds) – GSReddy Jul 30 '12 at 13:18
  • @GSReddy In the C# example I provided, the latitude and longitude are specified as decimal (`double` in C#), for instance 4.574554 (WGS84 system). – ken2k Jul 30 '12 at 13:22
  • Is this distance returned is in kilometers or meters? – GSReddy Jul 31 '12 at 05:15
  • @GSReddy In the C# example I provided, you can choose between miles or kilometers – ken2k Jul 31 '12 at 06:22
  • Thank you. It's working fine. Is there any idea how to implement Polygon geo fencing. I have googled it. I am getting alrogithums for convex polygons not for concave polygons. Suggest me. – GSReddy Aug 01 '12 at 05:30