Reputation: 289
I don't have much Math knowledge but I couldn't find why I am getting incorrect data. I am trying to find all the points on the circumference from the center vertex(lat, long) and radius 500 miles. I validate the result by passing the coordinates to google maps and get a list of states. The input lat is 41.8781 and long -87,6297 (Chicago IL) and the result I get is only the state IL
public List<Coordinates> PointOnCircle(double lat, double longi, float radius)
{
var r = radius / EARTH_RADIUS_NM;
var list = new List<Coordinates>();
for (int i = 0; i <= 360;)
{
Coordinates coordinates = new Coordinates();
// Convert from degrees to radians via multiplication by PI/180
double x = (r * Math.Cos(i * Math.PI / 180F)) + lat;
double y = (r * Math.Sin(i * Math.PI / 180F)) + longi;
coordinates.Latitude = x;
coordinates.Longitude = y;
i += 5;
list.Add(coordinates);
}
return list;
}
public Dictionary<string,string> GetStatesFromCordinates(List<Coordinates> coordinates)
{
var states = new Dictionary<string, string>();
foreach(var co in coordinates)
{
var geoLocation = GoogleService.GetGeoLocation(co.Latitude, co.Longitude);
var state= geoLocation.results.FirstOrDefault().address_components.FirstOrDefault(ac => ac.types.Any(ty => ty == "administrative_area_level_1"));
if(state != null)
{
var longstatename = state.long_name;
var shortstatename = state.short_name;
if(!states.ContainsKey(shortstatename))
states.Add(shortstatename, longstatename);
}
}
return states;
Upvotes: 2
Views: 482
Reputation: 39277
Your formula for calculating a delta angle based on a distance in miles is wrong. Whereas one degree of latitude is about 69 miles anywhere on the globe, one degree of longitude varies depending on the latitude. At the equator, one degree of longitude goes a long way, but at the poles, moving one degree of longitude isn't going to move you very far at all.
See https://gis.stackexchange.com/questions/142326/calculating-longitude-length-in-miles for a discussion of the appropriate formulae to use.
Upvotes: 1