I have some C# code that generates google maps. This codes looks at all the Points I need to plot on the map and then works out the Bounds of a rectangle to include those points. It then passes this bounds to the Google Maps API to set the zoom level appropriately to show all of the points on the map.
This code is working fine however I have a new requirement.
One of the points may have a precision associated with it. If this is the case then I draw a circle around the point with the radius set to the precision value. Again this works fine however my bounds checking is now not doing what I want it to do. I want to have the bounding box include the complete circle.
This requires an algorithm to take a point x and calculate the point y that would be z metres north of x and also z metres south of x.
Does anyone have this algorithm, preferably in C#. I did find a generic algorithm here but I appear to have not implemented this correctly as the answers I am getting are 1000s of km adrift.
This is the Generic example
Lat/lon given radial and distance
A point {lat,lon} is a distance d out on the tc radial from point 1 if:
lat=asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc))
IF (cos(lat)=0)
lon=lon1 // endpoint a pole
ELSE
lon=mod(lon1-asin(sin(tc)*sin(d)/cos(lat))+pi,2*pi)-pi
ENDIF
And this is my C# translation.
// Extend a Point North/South by the specified distance
public static Point ExtendPoint(Point _pt, int _distance, int _bearing )
{
Decimal lat = 0.0;
Decimal lng = 0.0;
lat = Math.Asin(Math.Sin(_pt.Lat) * Math.Cos(_distance) + Math.Cos(_pt.Lat) *
Math.Sin(_distance) * Math.Cos(_bearing));
if (Math.Cos(lat) == 0)
{
lng = _pt.Lng; // endpoint a pole
}
else
{
lng = (
(_pt.Lng - Math.Asin(Math.Sin(_bearing) * Math.Sin(_distance) / Math.Cos(lat))
+ Math.PI) % (2 * Math.PI)) - Math.PI;
}
ret = new Point(lat,lng);
return ret;
}
I am calling this function with a bearing of 0 to calculate the new northerly position and a value of 180 to calculate the new southerly position.
Can anyone either see what I have done wrong or perhaps provide a known working algorithm?
It is more accurate if you first reproject it to UTM and then check the distance.
Hope this helps
I have a very similar piece of code. It got me very close results when compared to another implementation.
I think the problem with yours is that you are using "distance" as linear distance in meters instead of angular distance in radians.
Where
and LatLonAlt is in degrees/meters (conversion takes place internally). Adjust as needed.
I assume you can figure out what the value for
UnitConstants.DegreesToRadians
is :)For what it's worth, I have an example in PHP which can do what the OP is requesting. In my example it is drawing a box around a starting lat/long coordinate, but the code can easily be used to get a single point, X number of km or miles away.
http://www.richardpeacock.com/blog/2011/11/draw-box-around-coordinate-google-maps-based-miles-or-kilometers
If you have a given latitude and longitude you can calculate the correct latitude and longitude of an x-km change in latitude like so:
The same can apply to longitude. If you have the total distance plus the change you can calculate the total degrees in a similar fashion.
Again, these calculations should work, but I'm running off pure intuition here, but the logic does seem to hold true.
Edit: As pointed out by Skizz 40,075 needs to be adjusted to the circumference of the earth at any given latitude using 2.pi.r.cos(lat) or 40074.cos(lat)
There are problems with the two equations on Ed William's rather awesome site... but I didn't analyze them to see why.
A third equation that I found here seems to give proper results.
Here is the test case in php... the third equation is correct, the first two give wildly incorrect values for longitude.
Note I recieved by email from the author (Ed Williams) of the first two equations:
For lazy people, (like me ;) ) a copy-paste solution, Erich Mirabal's version with very minor changes:
Usage: