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

 采纳的回答

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 个评论

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

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Mapping Toolbox 的更多信息

产品

版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by