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

 채택된 답변

Walter Roberson
Walter Roberson 2024년 12월 16일

0 개 추천

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

Andrija
Andrija 2024년 12월 17일
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

질문:

2024년 12월 16일

댓글:

2024년 12월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by