- Separate Pan and Click Events: Instead of using a single function for both click and pan events, consider using separate functions for each. This way, you can distinguish between mouse click and drag events.
- Reassigning Callbacks: Upon detecting a drag event, you can reassign the callback function to enable map translation functionality, allowing you to retain the ability to pan the map to points of interest.
- Event Handling: Ensure that the event handling within your callback functions is appropriately managed to differentiate between click, drag, and release events.
Dissocier les callback sur une carte
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Bonjour,
j'utilise le callback "buttondownfcn" pour récupérer les points cliqués sur une map.
Je peux toujour zoomer et dézoomer avec les boutons +/- affichés mais je ne peux plus "translater" la carte pour la repositionner sur les points d'intérêt. L'appel à la fonction associée à "buttondownfcn" bloque tous les clics sur la carte (enfin presque piusque je peux quand même dissocer clic gauche et droit) mais je ne peux pas dissocier le moment où on appuie du moment où on relève le bouton de clic de souris ni même le fait de glisser avec la souris (après je ne sais pas vers quelle fonction il faudrait rediriger pour conserver la fonction de translation de la carte)
Si quelqu'un a une solution, je suis preneur.
0 comentarios
Respuestas (1)
Lokesh
el 5 de Dic. de 2023
Hi Franck,
As per my understanding, you are encountering issues with panning functionality while using the "buttondownfcn" callback to retrieve clicked points on a map.
To address this issue, you can consider the following approach:
By separating the functionality for click and pan events and managing the event handling appropriately, you should be able to retain both the ability to retrieve clicked points and to pan the map to points of interest.
Below is a sample code that demonstrates how to handle the callbacks to retrieve clicked points while retaining the ability to pan:
% Create a sample plot (scatter plot) to represent the map
x = randn(1,100);
y = randn(1,100);
scatter(x, y, 'filled');
hold on;
% Initialize shared state
sharedState.dragging = false;
setappdata(gcf, 'SharedState', sharedState);
% Set up the buttondown function for the figure to handle click event
set(gcf, 'WindowButtonDownFcn', @mouseDownCallback);
% Set up the button motion function for the figure to handle pan event
set(gcf, 'WindowButtonMotionFcn', @mouseMotionCallback);
% Set up the button up function for the figure to handle release event
set(gcf, 'WindowButtonUpFcn', @mouseUpCallback);
function mouseUpCallback(src, event)
sharedState = getappdata(src, 'SharedState');
sharedState.dragging = false;
setappdata(src, 'SharedState', sharedState); % Update the shared state
% Add code to handle release event
disp('Mouse button released');
end
function mouseMotionCallback(src, event)
sharedState = getappdata(src, 'SharedState');
if sharedState.dragging
% Add code to handle panning/dragging of the map
% Example: Update the view based on the mouse movement
disp('Dragging the map');
end
end
function mouseDownCallback(src, event)
sharedState = getappdata(src, 'SharedState');
if strcmp(event.Source.SelectionType, 'normal') % Check for left mouse button click
sharedState.dragging = true; % Set dragging to true when the left mouse button is clicked
setappdata(src, 'SharedState', sharedState); % Update the shared state
pointClicked = get(gca, 'CurrentPoint'); % Retrieve the point clicked
disp(['Point clicked at: (', num2str(pointClicked(1,1)), ', ', num2str(pointClicked(1,2)), ')']);
disp('Left mouse button clicked');
end
end
The function "mouseUpCallback" manages the mouse button release event by updating the shared state. "mouseMotionCallback" manages mouse motion, checks the dragging status, and handles panning/dragging actions. Finally, "mouseDownCallback" updates the shared state upon a left mouse button click and retrieves clicked coordinates.
Please refer to the following MathWorks blog to know more about ‘WindowButtonMotionFcn’ callback:
I hope this resolves your query.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!