$("#mapDiv").fidget({ pinch: function(event, fidget){ switch(fidget.pinch.direction){ case "in": map.setLevel(map.getLevel() - 1); break; case "out": map.setLevel(map.getLevel() + 1); break; case "unknown": break; } } })
I would like to try and add the pinch and zoom to my app as well. I am not as experienced with building apps, but I have been able to work my way through some changes. Can you tell me where to add the code you posted for this and where the plugin code needs to go to achieve the pinch and zoom functionality? I am working with the "basic viewer" downloadable javascript template from arcgis online.
I appreciate the help! !
-Eric
<!DOCTYPE HTML> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <title>Title</title> <script> var dojoConfig = { //Required if you have dojo widgets (aka.dijits) in your HTML markup parseOnLoad: true }; </script> <!-- ArcGIS JSAPI 2.7, includes Dojo 1.6.1 and Dojo Widgets --> <link href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.7/js/dojo/dijit/themes/claro/claro.css" rel="stylesheet" type="text/css" /> <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.7/js/esri/dijit/css/Popup.css"/> <script src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.7" type="text/javascript"></script> <!-- jQuery Mobile 1.1.0-RC1 and jQuery 1.7.1 --> <link rel="stylesheet" href="jQuery/jquery.mobile-1.1.0-rc.1.min.css" /> <link rel="stylesheet" href="jQuery/jquery-mobile-custom.css" /> <!-- [YB 2012/02/29 We can use this file to modify the default css of jQuery Mobile] --> <script src="jQuery/jquery-1.7.1.min.js"></script> <!--<script src="jQuery/jQuery-mobile-custom.js"></script>--> <!-- [YB 2012/02/27 We can use this file to modify the global defaults of jQuery Mobile] --> <script type="text/javascript"> <!-- [YB 2012/03/02 I tried putting this custom script in an external js file, but it doesn't work with PhoneGap, mobileinit is not fired. I can't figure out why right now] --> // Edit jQuery Mobile global defaults $(document).bind("mobileinit", function(){ // Make sure a back button is always available on all pages $.mobile.page.prototype.options.addBackBtn=true; $.mobile.page.prototype.options.backBtnText = "Précédent"; $.mobile.page.prototype.options.backBtnTheme = "a"; $.mobile.page.prototype.options.backBtnIcon = "back"; // 1.1.0-RC1 forces "fade" for page transitions on Android. This code puts back the "slide" transition. // [YB 2012/03/02 : I've actually disabled all page transitions for now, to maximize performance. Might re-enable them later, if future jQueryMobile releases improves performance] $.mobile.defaultPageTransition = "none"; $.mobile.transitionFallbacks.slide = "none"; // Disable any effect when displaying dialogs $.mobile.defaultDialogTransition = "none"; // Disable tapToggle for headers and footers (DOESN'T WORK) $("[data-role=header]").fixedtoolbar({ tapToggle: false }); $("[data-role=footer]").fixedtoolbar({ tapToggle: false }); }); </script> <script src="jQuery/jquery.mobile-1.1.0-rc.1.min.js"></script> <!-- Mobiscroll plugin - DatePicker widget for mobiles --> <link href="plugins/mobiscroll/mobiscroll-1.5.3.min.css" rel="stylesheet" type="text/css" /> <script src="plugins/mobiscroll/mobiscroll-1.5.3.min.js" type="text/javascript"></script> <!-- Fidget plugins for additional touch gestures (e.g. pinch) --> <script src="plugins/fidget/jquery.fidget.beta.1.js" type="text/javascript"></script> <!-- PhoneGap 1.4.1 --> <script type="text/javascript" charset="utf-8" src="phonegap/phonegap-1.4.1.js"></script> <!-- Our stuff --> <link rel="stylesheet" href="index.css" /> <script type="text/javascript" charset="utf-8" src="extents.js"></script> <script type="text/javascript" charset="utf-8" src="index.js"></script> </head>
$(document).ready(function(){ $("#mapDiv").fidget({ pinch: function(event, fidget){ //var mapPoint = map.toMap(new esri.geometry.Point(event.x, event.y)); switch(fidget.pinch.direction){ case "in": map.setLevel(map.getLevel() - 1); fidget.pinch.direction = "unknown"; break; case "out": map.setLevel(map.getLevel() + 1); fidget.pinch.direction = "unknown"; break; //case "in": map.centerAndZoom(mapPoint, map.getLevel() - 1); break; //case "out": map.centerAndZoom(mapPoint, map.getLevel() + 1); break; case "unknown": break; } } }); });
Ok I am going to be trying to implement this over the next few days. I appreciate all the help! Certainly learning a great deal through this forum. I will let you know how it goes ... Good luck with your app!
-Eric
function onMapPinch(event, fidget){ if (fidget.pinch.status == "end"){ switch(fidget.pinch.direction){ case "in": map.setLevel(map.getLevel() - 1); fidget.pinch.direction = "unknown"; break; case "out": map.setLevel(map.getLevel() + 1); fidget.pinch.direction = "unknown"; break; case "unknown": break; } } else if (fidget.pinch.status == "move"){ event.preventDefault(); } //$("#pinchStatus").html($("#pinchStatus").html() + fidget.pinch.status + ", "); }
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
$("#mapDiv").fidget({ pinch: function(){ _this.onMapPinch.apply(_this, arguments); } });
MapPage.prototype.onMapPinch = function(event, fidget){ if (fidget.pinch.status == "end"){ switch(fidget.pinch.direction){ case "out": // Zoom in var midpoint = this.calculateMidpoint(fidget.pinch.touches); midpoint = this.map.toMap( new esri.geometry.Point(midpoint.x, midpoint.y) ); this.map.centerAndZoom(midpoint, this.map.getLevel() + 1); fidget.pinch.direction = "unknown"; break; case "in": // Zoom out this.map.setLevel(this.map.getLevel() - 1); fidget.pinch.direction = "unknown"; break; case "unknown": break; } } else if (fidget.pinch.status == "move" || fidget.pinch.status == "start"){ event.preventDefault(); } }; MapPage.prototype.calculateMidpoint = function(touches){ var midx = 0; var midy = 0; if (touches.length > 1) { midx = (touches[0].pageX + touches[1].pageX) / 2; midy = (touches[0].pageY + touches[1].pageY) / 2; midx -= $("#mapDiv").offset().left; midy -= $("#mapDiv").offset().top; } return { x: midx, y: midy }; };
function callPinchHandler() { if (defaults.pinch) { //if (defaults.zoomThis) { //var zoomFactor = calculateDistance() / fidget.pinch.startDistance; //console.log(fidget.pinch.distance + ' ' + calculateDistance()); //$this.css('webkitTransform', 'scale(' + zoomFactor + ')'); //} if (fidget.pinch.status == "start"){ fidget.pinch.touches = event.touches; // [YB 2012/05/10 : Save the positions so we can calculate the midpoint later, for our map pinch zoom] } defaults.pinch.call($this, event, fidget); } }