123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- define("dojox/geo/openlayers/TouchInteractionSupport", ["dojo/_base/kernel",
- "dojo/_base/declare",
- "dojo/_base/connect",
- "dojo/_base/html",
- "dojo/_base/lang",
- "dojo/_base/event",
- "dojo/_base/window"], function(dojo, declare, connect, html, lang, event, window){
- return declare("dojox.geo.openlayers.TouchInteractionSupport", null, {
-
-
-
-
- _map : null,
- _centerTouchLocation : null,
- _touchMoveListener : null,
- _touchEndListener : null,
- _initialFingerSpacing : null,
- _initialScale : null,
- _tapCount : null,
- _tapThreshold : null,
- _lastTap : null,
- constructor : function(/* OpenLayers.Map */map){
-
-
-
-
- this._map = map;
- this._centerTouchLocation = new OpenLayers.LonLat(0, 0);
- var div = this._map.div;
-
- connect.connect(div, "touchstart", this, this._touchStartHandler);
- connect.connect(div, "touchmove", this, this._touchMoveHandler);
- connect.connect(div, "touchend", this, this._touchEndHandler);
- this._tapCount = 0;
- this._lastTap = {
- x : 0,
- y : 0
- };
- this._tapThreshold = 100;
- },
- _getTouchBarycenter : function(touchEvent){
-
-
-
-
-
-
-
-
- var touches = touchEvent.touches;
- var firstTouch = touches[0];
- var secondTouch = null;
- if (touches.length > 1) {
- secondTouch = touches[1];
- } else {
- secondTouch = touches[0];
- }
- var marginBox = html.marginBox(this._map.div);
- var middleX = (firstTouch.pageX + secondTouch.pageX) / 2.0 - marginBox.l;
- var middleY = (firstTouch.pageY + secondTouch.pageY) / 2.0 - marginBox.t;
- return {
- x : middleX,
- y : middleY
- };
- },
- _getFingerSpacing : function(touchEvent){
-
-
-
-
-
-
-
-
- var touches = touchEvent.touches;
- var spacing = -1;
- if (touches.length >= 2) {
- var dx = (touches[1].pageX - touches[0].pageX);
- var dy = (touches[1].pageY - touches[0].pageY);
- spacing = Math.sqrt(dx * dx + dy * dy);
- }
- return spacing;
- },
- _isDoubleTap : function(touchEvent){
-
-
-
-
-
-
-
-
-
- var isDoubleTap = false;
- var touches = touchEvent.touches;
- if ((this._tapCount > 0) && touches.length == 1) {
-
- var dx = (touches[0].pageX - this._lastTap.x);
- var dy = (touches[0].pageY - this._lastTap.y);
- var distance = dx * dx + dy * dy;
- if (distance < this._tapThreshold) {
- isDoubleTap = true;
- } else {
- this._tapCount = 0;
- }
- }
- this._tapCount++;
- this._lastTap.x = touches[0].pageX;
- this._lastTap.y = touches[0].pageY;
- setTimeout(lang.hitch(this, function(){
- this._tapCount = 0;
- }), 300);
- return isDoubleTap;
- },
- _doubleTapHandler : function(touchEvent){
-
-
-
-
-
-
-
- var touches = touchEvent.touches;
- var marginBox = html.marginBox(this._map.div);
- var offX = touches[0].pageX - marginBox.l;
- var offY = touches[0].pageY - marginBox.t;
-
- var mapPoint = this._map.getLonLatFromPixel(new OpenLayers.Pixel(offX, offY));
-
- this._map.setCenter(new OpenLayers.LonLat(mapPoint.lon, mapPoint.lat), this._map.getZoom() + 1);
- },
- _touchStartHandler : function(touchEvent){
-
-
-
-
-
-
- event.stop(touchEvent);
-
- if (this._isDoubleTap(touchEvent)) {
- this._doubleTapHandler(touchEvent);
- return;
- }
-
- var middlePoint = this._getTouchBarycenter(touchEvent);
- this._centerTouchLocation = this._map.getLonLatFromPixel(new OpenLayers.Pixel(middlePoint.x, middlePoint.y));
-
- this._initialFingerSpacing = this._getFingerSpacing(touchEvent);
-
- this._initialScale = this._map.getScale();
-
- if (!this._touchMoveListener)
- this._touchMoveListener = connect.connect(window.global, "touchmove", this, this._touchMoveHandler);
- if (!this._touchEndListener)
- this._touchEndListener = connect.connect(window.global, "touchend", this, this._touchEndHandler);
- },
- _touchEndHandler : function(touchEvent){
-
-
-
-
-
-
- event.stop(touchEvent);
- var touches = touchEvent.touches;
- if (touches.length == 0) {
-
- if (this._touchMoveListener) {
- connect.disconnect(this._touchMoveListener);
- this._touchMoveListener = null;
- }
- if (this._touchEndListener) {
- connect.disconnect(this._touchEndListener);
- this._touchEndListener = null;
- }
- } else {
-
- var middlePoint = this._getTouchBarycenter(touchEvent);
- this._centerTouchLocation = this._map.getLonLatFromPixel(new OpenLayers.Pixel(middlePoint.x, middlePoint.y));
- }
- },
- _touchMoveHandler : function(touchEvent){
-
-
-
-
-
-
-
- event.stop(touchEvent);
- var middlePoint = this._getTouchBarycenter(touchEvent);
-
- var mapPoint = this._map.getLonLatFromPixel(new OpenLayers.Pixel(middlePoint.x, middlePoint.y));
- var mapOffsetLon = mapPoint.lon - this._centerTouchLocation.lon;
- var mapOffsetLat = mapPoint.lat - this._centerTouchLocation.lat;
-
- var scaleFactor = 1;
- var touches = touchEvent.touches;
- if (touches.length >= 2) {
- var fingerSpacing = this._getFingerSpacing(touchEvent);
- scaleFactor = fingerSpacing / this._initialFingerSpacing;
-
- this._map.zoomToScale(this._initialScale / scaleFactor);
- }
-
- var currentMapCenter = this._map.getCenter();
- this._map.setCenter(new OpenLayers.LonLat(currentMapCenter.lon - mapOffsetLon, currentMapCenter.lat
- - mapOffsetLat));
- }
- });
- });
|