Caculate lat/lon from distance

Given a start point, a distance and a heading, it is possible to work out the resulting latitude and longitude. See the maths behind it here.   See Radians Versus Degrees for the utility functions we are going to use in this calculation. There's an example of a mashup using this at Urbarama mashup

Variables

The variables required to calculate a new lat/lon are
  1. Origin Latitude and Longitude
  2. Heading in degrees from origin
  3. Distance in kilometers from origin

Together, they can be used to create the latitude and longitude of a new point like this


Code

You will find this code in the google apps script mcpher library or in the downloadable cDataSet.xlsm (usefulStuff module), at this gist, and below

Derquinse Gist.GitHub Gadget


Excel formula version

You may of course want to do all this in an Excel formula. Here's some data, in this case on Row 5.

calculate new latitude and longitude from distance and heading in Excel, VBA and Google Apps Script

new Latitude formula (cell e5)
=ASIN(SIN(A5*PI()/180)*COS(D5/6371)+COS(A5*PI()/180)*SIN(D5/6371)*COS(C5))*180/PI()

new Longitude formula (cell f5)
=((B5*PI()/180)+ATAN2(COS(D5/6371)- SIN(A5*PI()/180)*SIN(ASIN(SIN(A5*PI()/180)*COS(D5/6371)+COS(A5*PI()/180)*SIN(D5/6371) *COS(C5))),SIN(C5)*SIN(D5/6371)*COS(A5*PI()/180)))*180/PI()

new latitude formula calling VBA function (cell g5)
=getLatFromDistance(A5,D5,C5)

new longitude formula calling VBA function (cell h5)
=getLonFromDistance(A5,B5,D5,C5)

Take a look at One Liners for more tips like this. In the meantime why not join our forum,follow the blog or follow me on twitter to ensure you get updates when they are available. You can download this example and all other code in the cDataSet.xlsm from Downloads 

Formula attribution. 


For help and more information join our forumfollow the blogfollow me on twitter

You want to learn Google Apps Script?

Learning Apps Script, (and transitioning from VBA) are covered comprehensively in my my book, Going Gas - from VBA to Apps script, available All formats are available now from O'Reilly,Amazon and all good bookshops. You can also read a preview on O'Reilly

If you prefer Video style learning I also have two courses available. also published by O'Reilly.
Google Apps Script for Developers and Google Apps Script for Beginners.




Comments