how can I get the distance between two points in geoscatter

3 views (last 30 days)
Hello, so I got a variable of latitud and another of longitud and a latitud and langitud from a point in the map. What I want is to calculate the distance between each point of the variables to the point I got in geoscatter. How can I do it?

Answers (1)

Omega
Omega on 18 Sep 2023
Hi,
I understand that you would like to calculate distance between a reference point and a list of points in geoscatter.
Here’s sample MATLAB code to help you with the task:
% Define latitude and longitude arrays for your points
lat_array = [latitude1, latitude2, latitude3, ...]; % Replace with your values
lon_array = [longitude1, longitude2, longitude3, ...]; % Replace with your values
% Define the latitude and longitude of your reference point
ref_lat = reference_latitude; % Replace with your value
ref_lon = reference_longitude; % Replace with your value
% Convert latitude and longitude values to radians
lat_array_rad = deg2rad(lat_array);
lon_array_rad = deg2rad(lon_array);
ref_lat_rad = deg2rad(ref_lat);
ref_lon_rad = deg2rad(ref_lon);
% Radius of the Earth in kilometers (mean value)
earth_radius_km = 6371;
% Initialize an array to store distances
distances_km = zeros(size(lat_array));
% Calculate distances using the Haversine formula
for i = 1:length(lat_array)
dlat = lat_array_rad(i) - ref_lat_rad;
dlon = lon_array_rad(i) - ref_lon_rad;
a = sin(dlat/2)^2 + cos(ref_lat_rad) * cos(lat_array_rad(i)) * sin(dlon/2)^2;
c = 2 * atan2(sqrt(a), sqrt(1-a));
distances_km(i) = earth_radius_km * c;
end
The distances_km array contains the distances between the reference point and each point in your lat_array and lon_array.
To understand more, refer to the following documentation links:
I hope you find it useful.

Categories

Find more on Curve Fitting Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!