javascript Google Maps API v3 不会在地图加载后禁用滚轮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12058805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Maps API v3 won't disable scroll wheel after map loads
提问by Halceyon
I'm implementing google maps on a website and everything is working great, except that I can't seem to be able to disable the scrollwheel after the maps has loaded. If I set the option before the map loads to scrollwheel: false, then the scroll wheel is disabled, but if I try and do it later (I have a checkbox that enables/disables the scroll wheel).
我正在网站上实施谷歌地图,一切正常,除了在地图加载后我似乎无法禁用滚轮。如果我在地图加载到滚轮之前设置选项:false,则滚轮被禁用,但如果我稍后尝试执行此操作(我有一个启用/禁用滚轮的复选框)。
Here are my options for the google map on page load:
以下是我在页面加载时对谷歌地图的选项:
var myOptions = {
zoom: 15,
center: currentPosition,
draggable: true,
scrollwheel: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
and then after the click event has trigger on the checkbox, I have the following code to disable the scrollwheel. funny enough, the draggable = false is working and prevents me from dragging the map.
然后在单击事件在复选框上触发后,我有以下代码来禁用滚轮。有趣的是,draggable = false 正在工作并阻止我拖动地图。
var checked = $('#chkPin').is(':checked');
log("map active: " + checked);
if (checked) {
map.scrollwheel = false;
map.draggable = false;
map.zoomControl = false;
} else {
map.scrollwheel = true;
map.draggable = true;
map.zoomControl = true;
}
回答by Chad Killingsworth
Editing undocumented properties on Maps API objects is not supported and can lead to unpredictable results. You shouldn't directly modify properties on a map object. Instead, modify the properties using one of the documented options:
不支持在 Maps API 对象上编辑未记录的属性,这可能会导致不可预测的结果。您不应直接修改地图对象的属性。相反,使用记录的选项之一修改属性:
Object specific defined getters/setters:
对象特定定义的 getter/setter:
map.setOptions({'scrollwheel': false});
MVCObject generic getters/setters:
MVCObject 通用 getter/setter:
map.set('scrollwheel', false);
var isScrollWheelEnabled = map.get('scrollwheel');
Both of these options successfully disabled scrollwheel zooming of the map after it had already been initialized.
这两个选项都在地图初始化后成功禁用了地图的滚轮缩放。