File Exchange

image thumbnail

LatLon distance

version (1.64 KB) by M Sohrabinia
Find distance between two points based on latlon coordinates


Updated 30 Oct 2012

View License

This functions finds distance (in km) between two points on Earth using latitude-longitude coordinates of the two points. Distance is calculated using two distance funstions: Haversine and Pythagoran. The first output is based on Haversine function, which is more accurate especially for longer distances.

latlon1: latlon of origin point [lat lon]
latlon2: latlon of destination point [lat lon]

d1km: distance calculated by Haversine formula
d2km: distance calculated based on Pythagoran theorem

--Example 1, short distance:
latlon1=[-43 172];
latlon2=[-44 171];
[d1km d2km]=distance(latlon1,latlon2)
d1km =
137.365669065197 (km)
d2km =
137.368179013869 (km)
%d1km approximately equal to d2km

--Example 2, longer distance:
latlon1=[-43 172];
latlon2=[20 -108];
[d1km d2km]=distance(latlon1,latlon2)
d1km =
10734.8931427602 (km)
d2km =
31303.4535270825 (km)
d1km is significantly different from d2km (d2km is not able to work for longer distances).

Cite As

M Sohrabinia (2021). LatLon distance (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (24)


why doesn't it work for this below calculation?

[d, ~] = lldistkm([-8, -25], [8, 155] )

Michael Pietruschka

David Franco

Sejoon Ahn

Thank you so much!

David Mayers

There are also the built in functions 'distance' and 'deg2km' which yield identical results to this function in all cases that I've tried



Thank you! It works great!

Babita Jangir



Works perfectly.

Vardenis Pavardenis

Manoj Pai

How to apply inputs

Maninder Ade

Aysu Özel

M Sohrabinia

@Seb: decimal degrees as shown in the examples. Thanks for rating the code.

M Sohrabinia

@Justin: thanks for the suggestions Justin, I will try to get back to this, have been away from Matlab for the last couple of years. I acknowledge I have not spent a lot work on handling the exceptions, the aim was to calculate the correct distance which I believe it does.

Aticha Boousuwan

Rafael D

sandeep manjanna

Seb Biass


Just a question, are inputs in deg min sec or decimal degrees?



Straight forward. Handy function for GPS users

Justin Winokur

This code does what it says it should but there are two 2-minute changes to make it much better:

1) make all operations vectorized so you can put a list of lat,lon coordinates. Basically, add a "." before everything. Super easy and much better

2) Add a check for NaN's. If two points are too close, you get NaN when it should be zero. Very easy to do:

>> d1km(isnan(d1km)) = 0;

I hope this helps

Tianyang Li

Ian Shapiro

Simple but effective. Does just what it needs to do.

MATLAB Release Compatibility
Created with R2008b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: pathdist

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!