javascript - Custom Zoom Control Results In Maximum Call Stack Size Exceeded Error -
i'm trying change zoom increment when using google maps javascript api on ios. example, instead of going zoom 14 zoom 15, go straight zoom 16 , zoom 18, , on... when user uses pinch zoom gesture.
i know can achieve buttons, can't seem find solution pinch zoom gesture. realize zoom_changed
event needs tracked.
i error of maximum call stack size exceeded
map.setzoom(zoom + 2);
, map.setzoom(zoom - 2);
when added on line 17 , 19, respectively.
check out jsfiddle: http://jsfiddle.net/2zmn9173/4/. part need with:
if (useragent.indexof('iphone') != -1) { google.maps.event.addlistener(map, 'zoom_changed', function () { if (map.getzoom() > zoom) { alert("zoomed in level " + zoom); } else if (map.getzoom() < zoom) { alert("zoomed out level " + zoom); } zoom = map.getzoom(); }); }
the buttons on top demonstrate functionality trying achieve, pinch zoom gesture.
simple approach:
only set new zoom when current zoom not number(this should stop infinite loop)
google.maps.event.addlistener(map,'zoom_changed',function(){ var z=this.getzoom(); if(z%2){//when zoom odd number var fx=((!isnan(this.get('z'))&&this.get('z')<z))?2:0; this.setzoom(math.floor(z/2)*2+fx); }else{ this.set('z',z); } });
demo:
function initialize() { var ctrl = document.getelementbyid('zoom'); map = new google.maps.map(document.getelementbyid('map_canvas'), { zoom: 14, center: new google.maps.latlng(52.549, 13.425), noclear: true }), map.controls[google.maps.controlposition.top_center].push(ctrl); google.maps.event.addlistener(map, 'zoom_changed', function() { var z = this.getzoom(); if (z % 2) { //when zoom odd number var fx = ((!isnan(this.get('z')) && this.get('z') < z)) ? 2 : 0; this.setzoom(math.floor(z / 2) * 2 + fx); } else { this.set('z', z); } ctrl.innerhtml = this.getzoom(); }); google.maps.event.trigger(map, 'zoom_changed'); } google.maps.event.adddomlistener(window, 'load', initialize);
html, body, #map_canvas { height: 100%; margin: 0; padding: 0 } #zoom { font: bold 2em monospace; background: tomato; width: 30px; text-align: center; }
<script src="https://maps.googleapis.com/maps/api/js?v=3"></script> <div id="map_canvas"> <div id="zoom"></div> </div>
Comments
Post a Comment