In the older version 3.x of the ArcGIS Server JavaScript API, it was possible to disable map navigation using disableMapNavigation()
as explained in Disabling map in ArcGIS API for Javascript
What is the equivalent for disabling map navigation in version 4.x of the ArcGIS Server JavaScript API? The use-case is to prevent map navigation temporarily, while an external query is running.
The mapView has a navigation property which allows certain types of navigation to be disabled, as in the example at https://developers.arcgis.com/javascript/latest/sample-code/sandbox/index.html?sample=view-disable-navigation, but this doesn't seem to allow all navigation to be disabled.
Eg, even when using:
navigation: {
gamepad: {enabled: false},
browserTouchPanEnabled: false,
momentumEnabled: false,
mouseWheelZoomEnabled: false
}
the user is able to pan the map with the mouse, zoom by double-clicking, rotate the map using the right-mouse-button, etc.
-
1This was a very interesting subject to study, it's weird that the option to completely disable navigation was removed with the new API version.Dror Bogin– Dror Bogin2020年11月16日 07:00:13 +00:00Commented Nov 16, 2020 at 7:00
2 Answers 2
It seems to completely disable map navigation (under a condition, like you asked) you would need to stop event propagation from certain events.
view.on(["click", "drag", "double-click", "mouse-wheel", "hold", ], function(event) {
if(thinking){
event.stopPropagation();
}
});
This will stop all of those events, if you want to disable a click or drag event from just the right mouse button (in case you want to disable just rotation for example), then the click
,drag
and double-click
events also have a button property which you can use:
Value Description
0 left click (or touch)
1 middle click
2 right click
*Taken from the view event documentation
EDIT
After asking about Disabling Shift+Left-click+Drag
zooming as well, I went back to the docs and found this example
Apparently you can add the definition of a pressed keyboard key to the event to stop the event from bubbling.
view.on("drag", ["Shift"], function(event) {
event.stopPropagation();
});
view.on("drag", ["Shift", "Control"], function(event) {
event.stopPropagation();
});
-
edited my answer, added more instructions about ignoring all mouse eventsDror Bogin– Dror Bogin2020年11月16日 06:58:59 +00:00Commented Nov 16, 2020 at 6:58
-
Yeah, I know, I added that because you wanted to turn the navigation on and off. otherwise you can just create a non interactive map with turning all map events off.Dror Bogin– Dror Bogin2020年11月16日 09:22:07 +00:00Commented Nov 16, 2020 at 9:22
-
I just noticed that this doesn't prevent
Shift+Left-click+Drag: Zoom to extent of drawn graphic
(developers.arcgis.com/javascript/latest/api-reference/…) Any further suggestions? Thanks codepen.io/slead/pen/poEEjwZStephen Lead– Stephen Lead2020年12月09日 01:31:24 +00:00Commented Dec 9, 2020 at 1:31 -
1Adding to the answerDror Bogin– Dror Bogin2020年12月09日 04:08:39 +00:00Commented Dec 9, 2020 at 4:08
-
You are truly the master of finding gold buried in the documentation. Many thanks againStephen Lead– Stephen Lead2020年12月09日 05:18:58 +00:00Commented Dec 9, 2020 at 5:18
In addition to Dror's excellent answer, to prevent the user from using the Zoom In/Out buttons you can create a Zoom widget which can then be removed as necessary.
Remove the standard Zoom when instantiating the view using the ui keyword, and don't include the Zoom control:
const view = new MapView({
container: "viewDiv",
map: map,
ui: { components: ["attribution"] }
});
Then add/remove a Zoom control as required:
let zoomControl = new Zoom({
view: view
});
// Enable zoom
view.ui.add(zoomControl, {
position: "top-left"
});
// Disable zoom
view.ui.remove(zoomControl);
Explore related questions
See similar questions with these tags.