我使用谷歌地图API (v3)在页面上绘制一些地图。我想做的一件事是当你在地图上滚动鼠标滚轮时禁用缩放,但我不知道该怎么做。
我已经禁用了scaleControl(即删除缩放UI元素),但这并不能阻止滚轮缩放。
下面是我的函数的一部分(这是一个简单的jQuery插件):
$.fn.showMap = function(options, addr){
options = $.extend({
navigationControl: false,
mapTypeControl: false,
scaleControl: false,
draggable: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
}, options);
var map = new google.maps.Map(document.getElementById($(this).attr('id')), options);
// Code cut from this example as not relevant
};
我用这个简单的例子来做
jQuery
$('.map').click(function(){
$(this).find('iframe').addClass('clicked')
}).mouseleave(function(){
$(this).find('iframe').removeClass('clicked')
});
CSS
.map {
width: 100%;
}
.map iframe {
width: 100%;
display: block;
pointer-events: none;
position: relative; /* IE needs a position other than static */
}
.map iframe.clicked {
pointer-events: auto;
}
或者使用gmap选项
function init() {
var mapOptions = {
scrollwheel: false,
截至目前(2017年10月),谷歌已经实现了一个特定的属性来处理缩放/滚动,称为手势处理。它的目的是处理移动设备的操作,但它也修改了桌面浏览器的行为。以下是官方文件:
function initMap() {
var locationRio = {lat: -22.915, lng: -43.197};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 13,
center: locationRio,
gestureHandling: 'none'
});
The available values for gestureHandling are:
'greedy': The map always pans (up or down, left or right) when the user swipes (drags on) the screen. In other words, both a one-finger swipe and a two-finger swipe cause the map to pan.
'cooperative': The user must swipe with one finger to scroll the page and two fingers to pan the map. If the user swipes the map with one finger, an overlay appears on the map, with a prompt telling the user to use two fingers to move the map. On desktop applications, users can zoom or pan the map by scrolling while pressing a modifier key (the ctrl or ⌘ key).
'none': This option disables panning and pinching on the map for mobile devices, and dragging of the map on desktop devices.
'auto' (default): Depending on whether the page is scrollable, the Google Maps JavaScript API sets the gestureHandling property to either 'cooperative' or 'greedy'
简而言之,您可以轻松地将设置强制为“始终可缩放”(“贪婪”)、“永不可缩放”(“none”)或“用户必须按CRTL/⌘才能启用缩放”(“合作”)。