Input a geographic coordinate, return a coordinate within x miles of that inputted coordinate - C# .NET

119 views Asked by At

As the title suggests, I am trying to generate a coordinate based on another coordinate that is within an x mile (or whichever unit is most convenient) radius of the inputted one.

As an example:

  • I am given a geographic coordinate (lat, lon) of 39.083056, -94.820200.
  • I want to be returned another set of coordinates that is within a x miles radius of that coordinate, such as 39.110998, -94.799668.
  • The x mile radius isn't as important as the fact that the returned coordinates are within that x mile radius.

I have searched and searched, but I must be searching the wrong thing because all the posts that I have been able to find seem like they get very close to what I am trying to do but aren't quite hitting the nail on the head.

1

There are 1 answers

3
NPras On BEST ANSWER

I'm sorry you're being downvoted to oblivion. I understand it can be frustrating trying to search for something without knowing what exactly to search for.

You may be interested in Orthodromic Lines/Distances: wiki. If this answer doesn't fulfil your needs, at least you have a new term to google and hopefully will lead you to one that does suit.

You could try using the Geo library. Its documentation is on the sparse side, but it does contain a method that could be useful to you: CalculateOrthodromicLine(startPoint, heading, distance)

A pseudocode would be something as simple as this:

var startPoint = new Coordinate(lat, long);
var heading = Random between 0 and 360 degrees
var distance = Random between 0 and X metres
var endPoint = //<-- et voila!
    GeoContext.Current.GeodeticCalculator
    .CalculateOrthodromicLine(startPoint, heading, distance)
    .Coordinate2;

Edit: As mentioned in the wiki, the Earth is not a perfect sphere, but a spheroid instead. The library's GeoContext.Current by default uses its Spheroid calculations, so you should be okay.

Good luck!