Check whether a point(lat/long) is within a circle(Centre lat/long known)
I am trying to see if a given pair of (lat,long) will fall within a circular region. The centre of the circle is known (lat,long values) as well as the radius. The approcah I have used is:
- Calculate the distance between the centre and the given lat/long using Haversines formula.
Formula:
a = ( sin(delta_lat/2) )^2 + cos (vp_Current.v_Latitude) *
cos(vp_CentreOfCircle.v开发者_Go百科_Latitude) * ( sin(delta_long/2) )^2;
c = 2 * atan2( sqrt(a), sqrt(1-a) );
d = R * c;
where: R = 6371 Km.
, delta_lat = lat2 - lat1
, delta_long = long2 - long1
- Then check if this distance is less than the radius to see if it is within the circle.
I have written the code in C but when I enter the following data the output says that the point is outside rather than within the circle (the point is within as I checked on google maps).
Centre(lat/long) = (19.228177, 72.685547)
Given point = (18.959999, 72.819999)
Radius = 30 miles (about 49 Km but entered as 50 in the program).
The weird thing is if I enter the radius as 5000, the output says inside but not even for 500. I don't know where the problem is..would be really grateful if anyone could share some pointers...thanks.
(I am nowhere an expert in all this, but it was fun to look around for education)
Since the argument for the Harvesin function is in radians, should the latitude and longitude be converted from degrees to radians ?
To start, I'd check to see what your distance between the 2 points (center & given) ends up being. Then I'd make sure you're using all the correct units (meters v. miles, radians v. degrees).
If you don't find a mistake there, I'd start looking at your Haversine implementation vs. one of the posted ones on wikipedia.
Hope that helps.
精彩评论