how can I get the distance between two points in geoscatter

2 ビュー (過去 30 日間)
flashpode
flashpode 2022 年 2 月 1 日
回答済み: Divit 2023 年 9 月 18 日
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?

回答 (1 件)

Divit
Divit 2023 年 9 月 18 日
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.

カテゴリ

Help Center および File ExchangeMap Display についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by