Geoaxes Interactive Drawing Issue

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

 Accepted Answer

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 Comment

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

Sign in to comment.

More Answers (0)

Products

Release

R2024b

Asked:

on 16 Dec 2024

Commented:

on 17 Dec 2024

Community Treasure Hunt

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

Start Hunting!