Created
October 2, 2012 13:01
-
-
Save brucemcpherson/3818910 to your computer and use it in GitHub Desktop.
lat/lon distance in GAS and VBA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// see http://www.movable-type.co.uk/scripts/latlong.html for maths | |
/** | |
* given lat, distance in km, and heading calculate new lat | |
* @param {number} mLat origin latitude | |
* @param {number} d distance in km | |
* @param {number} heading heading (degrees) | |
* @return {number} destination lat | |
*/ | |
function getLatFromDistance(mLat, d , heading ) { | |
var lat = toRadians(mLat); | |
var newLat = Math.asin(Math.sin(lat) * | |
Math.cos(d / earthRadius()) + | |
Math.cos(lat) * | |
Math.sin(d / earthRadius()) * | |
Math.cos(heading)); | |
return fromRadians(newLat); | |
} | |
/** | |
* given lat,lon, distance in km, and heading calculate new lon | |
* @param {number} mLat origin latitude | |
* @param {number} mLon origin longitude | |
* @param {number} d distance in km | |
* @param {number} heading heading (degrees) | |
* @return {number} destination lat | |
*/ | |
function getLonFromDistance(mLat, mLon, d, heading) { | |
var lat = toRadians(mLat); | |
var lon = toRadians(mLon); | |
var newLat = toRadians(getLatFromDistance(mLat, d, heading)); | |
var newLon = lon + | |
Math.atan2( | |
Math.sin(heading) * Math.sin(d / earthRadius()) * Math.cos(lat), | |
Math.cos(d / earthRadius()) - (Math.sin(lat) * Math.sin(newLat)) | |
); | |
return fromRadians (newLon); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' see http://www.movable-type.co.uk/scripts/latlong.html for maths | |
' VBA version | |
Public Function getLatFromDistance(mLat As Double, d As Double, heading As Double) As Double | |
Dim lat As Double | |
' convert ro radians | |
lat = toRadians(mLat) | |
getLatFromDistance = _ | |
fromRadians( _ | |
Application.WorksheetFunction.Asin(sIn(lat) * _ | |
Cos(d / earthRadius) + _ | |
Cos(lat) * _ | |
sIn(d / earthRadius) * _ | |
Cos(heading))) | |
End Function | |
Public Function getLonFromDistance(mLat As Double, mLon As Double, d As Double, heading As Double) As Double | |
Dim lat As Double, lon As Double, newLat As Double | |
' convert ro radians | |
lat = toRadians(mLat) | |
lon = toRadians(mLon) | |
newLat = toRadians(getLatFromDistance(mLat, d, heading)) | |
getLonFromDistance = _ | |
fromRadians( _ | |
(lon + Application.WorksheetFunction.Atan2(Cos(d / earthRadius) - _ | |
sIn(lat) * _ | |
sIn(newLat), _ | |
sIn(heading) * _ | |
sIn(d / earthRadius) * _ | |
Cos(lat)))) | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See http://ramblings.mcpher.com/Home/excelquirks/getmaps/trig for details