I ran into an interesting problem recently. I was working with latitude-longitude data. I had a Bing Maps latitude-longitude point, and a direction (or bearing), and a distance (in km), and I wanted to determine what the resulting latitude-longitude point would be. I went to one of my favorite math sites, Ask Dr. Math, and after a bit of searching found the equations I needed. Here is a JavaScript version of the function:

function LatLonOf(latLon, dist, nsew)

{

// given a VELatLon point latLon, a distance (in km),

// and a direction (‘N’, ‘S’, ‘E’, or ‘W’),

// return the VELatLong

// see http://mathforum.org/library/drmath/view/51816.html for the math

var lat = (latLon.Latitude * Math.PI) / 180; // in radians

var lon = (latLon.Longitude * Math.PI) / 180; // in radians

var r = 6371; // radius of earth in km

var d = dist / r; // convert dist to arc radians

var tc; // heading in radians

if (nsew == ‘N’) tc = 0;

else if (nsew == ‘S’) tc = Math.PI;

else if (nsew == ‘E’) tc = Math.PI / 2;

else if (nsew == ‘W’) tc = 3 * Math.PI / 2;

var resultLat, resultLon;

resultLat = Math.asin(Math.sin(lat) * Math.cos(d) +

Math.cos(lat) * Math.sin(d) * Math.cos(tc));

var dlon = Math.atan2(Math.sin(tc) * Math.sin(d) *

Math.cos(lat), Math.cos(d) – Math.sin(lat) * Math.sin(lat));

resultLon = ((lon + dlon + Math.PI) % (2 * Math.PI)) – Math.PI;

resultLat = (resultLat * 180) / Math.PI; // back to degrees

resultLon = (resultLon * 180) / Math.PI;

return new VELatLong(resultLat, resultLon);

}

I’ve tested the function a bit and it seems to work. For example,

var seattle = new VELatLong(47.60, -122.33);

var latlon = LatLonOf(seattle, 100.0, ‘N’);

var dist = Distance(seattle, latlon);

alert(dist);

First I set up the latitude-longitude of Seattle. Then I compute the latitude-longitude of the point 100 km due north. Then I compute the distance between the new latitude-longitude and Seattle using a Distance function (you can also find this on the Dr. Math site). The result is 100.0 as expected.

You can see the LatLonOf function has some pretty nasty math. To be honest I didn’t analyze the equations because I didn’t have time, but I’ll do so someday.