MATLAB: How to plot a map and calculate distance between two points in the Geographic Space

arclenaxesmdeg2kmdistancegeographicgeoshowgpslandareasmapMATLABpcarreeplotpointsprojectionspace

How can I show a map in a figure? I want the map to be draggable.
How can I calculate the distance between two points in a Geographic Space? I have a longitude and latitude point I want to plot in a map.

Best Answer

Showing the map in a figure There are different projections as shown in documentation page in https://www.mathworks.com/help/map/summary-and-guide-to-projections.html. You can choose the most appropriate for your workflow.
You can plot latitude and longitude points, e.g. 52.229588, 0.150855, using "geoshow" function as shown in documentation page in https://www.mathworks.com/help/map/ref/geoshow.html. Here is an example:
>> figure
>> geoax = axesm('pcarree');
>> geoshow('landareas.shp', 'FaceColor', [0.5 1.0 0.5]);
>> geoshow(52.229588, 0.150855, 'DisplayType', 'Point', 'Marker', '+', 'Color', 'red');
>> geoshow(42.301853, -71.376126, 'DisplayType', 'Point', 'Marker', '+', 'Color', 'red');
The figure will have a map which is draggable, and will have a red cross in Cambridge, UK. The figure allows to zoom in closer, to zoom out and to drag around the map.
Distance between two points in the Geographic Space You can calculate the distance between two points, as shown in documentation page in https://www.mathworks.com/help/map/calculate-distance-between-two-points-in-geographic-space.html, https://www.mathworks.com/help/map/ref/distance.html.
For the example, let us choose two GPS points:
  • 52.229588, 0.150855 in Cambridge, UK.
  • 42.301853, -71.376126 in Natick, MA, USA.
Then the arc length distance can be computed in this way:
[arclen, az] = distance([52.229588, 0.150855], [42.301853, -71.376126])
arclen =
47.5027
az =
287.9302
Then the distance can be calculated in kilometres using "deg2km" function as shown in documentation page in https://www.mathworks.com/help/map/ref/deg2km.html.
>> km = deg2km(arclen)
km =
5.2821e+03