I created fucntion that plots something (in this example, it is sinusoid, but it doesnt matter) and by holding left button on mouse i can draw over it. Now, I want the same thing, but when I use geoplot in geoaxes, but I don't know how to do it. Can someone help me?
Example of code with regular axes:
function draw_on_figure()
x = linspace(0, 2*pi, 100);
y = sin(2*x);
figure('WindowButtonDownFcn', @startDrawing, ...
'WindowButtonMotionFcn', @draw, ...
'WindowButtonUpFcn', @stopDrawing);
plot(x, y, 'b', 'LineWidth', 2);
hold on;
axis([0 2*pi -1.5 1.5]);
grid on;
isDrawing = false;
lineHandle = [];
function startDrawing(~, ~)
if strcmp(get(gcf, 'SelectionType'), 'normal') % normal - left click
isDrawing = true;
currentPoint = get(gca, 'CurrentPoint');
lineHandle = line(currentPoint(1, 1), currentPoint(1, 2), ...
'Color', 'r', 'LineWidth', 2);
end
end
function draw(~, ~)
if isDrawing
% current position of the mouse
position = get(gca, 'CurrentPoint');
x = get(lineHandle, 'XData');
y = get(lineHandle, 'YData');
set(lineHandle, 'XData', [x, position(1, 1)], ...
'YData', [y, position(1, 2)]);
end
end
function stopDrawing(~, ~)
isDrawing = false;
lineHandle = [];
end
end

 Réponse acceptée

Walter Roberson
Walter Roberson le 16 Déc 2024
I suspect the code would look like this:
%assumes the geoplot already exists
function draw_on_geofigure()
fig = gcf;
set(fig, 'WindowButtonDownFcn', @startDrawing, ...
'WindowButtonMotionFcn', @draw, ...
'WindowButtonUpFcn', @stopDrawing);
isDrawing = false;
lineHandle = [];
function startDrawing(~, ~)
if strcmp(get(gcf, 'SelectionType'), 'normal') % normal - left click
isDrawing = true;
currentPoint = get(gca, 'CurrentPoint');
lineHandle = geoplot(currentPoint(1, 1), currentPoint(1, 2), ...
'Color', 'r', 'LineWidth', 2);
end
end
function draw(~, ~)
if isDrawing
% current position of the mouse
position = get(gca, 'CurrentPoint');
x = get(lineHandle, 'LongitudeData');
y = get(lineHandle, 'LatitudeData');
set(lineHandle, 'LongitudeData', [x, position(1, 1)], ...
'LatitudeData', [y, position(1, 2)]);
end
end
function stopDrawing(~, ~)
isDrawing = false;
lineHandle = [];
end
end

1 commentaire

Andrija
Andrija le 17 Déc 2024
Thank you for idea! I changed some thihngs and now it works perfect. Code example below:
function draw_on_geofigure()
fig = gcf;
set(fig, 'WindowButtonDownFcn', @startDrawing, ...
'WindowButtonMotionFcn', @draw, ...
'WindowButtonUpFcn', @stopDrawing);
geoplot(20.12345,19.223366,'rx','MarkerSize',10,'LineWidth',1.5)
hold on
geolimits([20 21],[19 20])
isDrawing = false;
lineHandle = [];
function startDrawing(~, ~)
if strcmp(get(gcf, 'SelectionType'), 'normal') % normal - left click
isDrawing = true;
currentPoint = get(gca, 'CurrentPoint');
lineHandle = geoplot(currentPoint(1, 1), currentPoint(1, 2), ...
'Color', 'r', 'LineWidth', 2);
end
end
function draw(~, ~)
if isDrawing
% current position of the mouse
position = get(gca, 'CurrentPoint');
x = get(lineHandle, 'LongitudeData');
y = get(lineHandle, 'LatitudeData');
set(lineHandle, 'LongitudeData', [x, position(1, 2)], ...
'LatitudeData', [y, position(1, 1)]);
end
end
function stopDrawing(~, ~)
isDrawing = false;
lineHandle = [];
end
end

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by