- deg2rad: https://www.mathworks.com/help/matlab/ref/deg2rad.html
- atan2: https://www.mathworks.com/help/matlab/ref/atan2.html
- geoscatter: https://www.mathworks.com/help/matlab/ref/geoscatter.html
how can I get the distance between two points in geoscatter
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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?
0 Kommentare
Antworten (1)
Divit
am 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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Map Display finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!