[英]Modifying zoom_changed event to zoom a map by 2 levels
How can I modify my code for the zoom_changed
event to support zooming in and out by levels of 2? 如何修改zoom_changed
事件的代码以支持2级放大和缩小? For example, I would prefer the map to go from 11 to 13 to 15, and so on. 例如,我希望地图从11变为13到15,依此类推。 Basically, odd level zooms only. 基本上,仅奇数级缩放。
My current implementation results in Uncaught RangeError: Maximum call stack size exceeded
error because the event keeps firing. 我当前的实现导致Uncaught RangeError: Maximum call stack size exceeded
由于事件不断触发,导致Uncaught RangeError: Maximum call stack size exceeded
错误。
I know I can create custom zoom in/out buttons (as demonstrated by this solution ), but this type of zoom functionality would only work if someone used those buttons. 我知道我可以创建自定义放大/缩小按钮(如本解决方案所示 ),但是这种缩放功能仅在有人使用这些按钮时才起作用。
I'm hoping for this zoom functionality to work for scrollwheel and pinch to zoom in/out on mobile as well. 我希望此缩放功能能够在滚轮上运行,并在移动设备上同时放大/缩小。
Here's a sample of my current implementation: http://jsfiddle.net/gbrdn0sn 这是我当前的实现示例: http : //jsfiddle.net/gbrdn0sn
code snippet: 代码段:
var currentZoom; function initialize() { var mapOptions = { zoom: 11, center: { lat: -33.8666, lng: 151.1958 } }; var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); google.maps.event.addListener(map, 'zoom_changed', function() { currentZoom = map.getZoom() + 2; console.log(currentZoom); map.setZoom(currentZoom); document.getElementById('zoom-level').innerHTML = currentZoom; }); } google.maps.event.addDomListener(window, 'load', initialize);
html, body { height: 100%; margin: 0; padding: 0px } #map-canvas { height: 100%; width: 100%; } #current-zoom { display: block; width: 100%; }
<script src="https://maps.googleapis.com/maps/api/js"></script> <div id="zoom-level"></div> <div id="map-canvas"></div>
To prevent the error is to prevent the event from firing on the commanded "extra zoom". 防止错误是为了防止事件在命令的“额外缩放”上触发。 One way to do that is to ignore the commanded "zoom_changed" event. 一种方法是忽略命令的“ zoom_changed”事件。
google.maps.event.addListenerOnce(map, 'zoom_changed', zoomChanged); google.maps.event.addListenerOnce(map,'zoom_changed',zoomChanged);
function zoomChanged() {
// ignore zoom_changed event from this change
google.maps.event.addListenerOnce(map, 'zoom_changed', function() {
// then process the next
google.maps.event.addListenerOnce(map, 'zoom_changed', zoomChanged);
});
console.log("lastZoom=" + currentZoom + " map.getZoom()=" + map.getZoom());
if (currentZoom < map.getZoom()) { // zooming in
if (map.getZoom() % 2 != 1) {
currentZoom = map.getZoom() + 1;
} else {
currentZoom = map.getZoom();
}
} else { // zooming out
if (map.getZoom() % 2 != 1) {
currentZoom = map.getZoom() - 1;
} else {
currentZoom = map.getZoom() - 2;
}
}
console.log(currentZoom);
map.setZoom(currentZoom);
currentZoom = map.getZoom();
document.getElementById('zoom-level').innerHTML = currentZoom;
}
code snippet: 代码段:
var currentZoom; function initialize() { var mapOptions = { zoom: 11, center: { lat: -33.8666, lng: 151.1958 } }; var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); currentZoom = map.getZoom(); google.maps.event.addListenerOnce(map, 'zoom_changed', zoomChanged); function zoomChanged() { // ignore zoom_changed event from this change google.maps.event.addListenerOnce(map, 'zoom_changed', function() { // then process the next google.maps.event.addListenerOnce(map, 'zoom_changed', zoomChanged); }); console.log("lastZoom=" + currentZoom + " map.getZoom()=" + map.getZoom()); if (currentZoom < map.getZoom()) { // zooming in if (map.getZoom() % 2 != 1) { currentZoom = map.getZoom() + 1; } else { currentZoom = map.getZoom(); } } else { // zooming out if (map.getZoom() % 2 != 1) { currentZoom = map.getZoom() - 1; } else { currentZoom = map.getZoom() - 2; } } console.log(currentZoom); map.setZoom(currentZoom); currentZoom = map.getZoom(); document.getElementById('zoom-level').innerHTML = currentZoom; } } google.maps.event.addDomListener(window, 'load', initialize);
html, body { height: 100%; margin: 0; padding: 0px } #map-canvas { height: 100%; width: 100%; } #current-zoom { display: block; width: 100%; }
<script src="https://maps.googleapis.com/maps/api/js"></script> <div id="zoom-level"></div> <div id="map-canvas"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.