| 1 |
define([ |
| 2 |
"dojo/_base/declare", |
| 3 |
"dojo/_base/connect", |
| 4 |
"dojo/_base/html", |
| 5 |
"dojo/_base/lang", |
| 6 |
"dojo/_base/event", |
| 7 |
"dojo/_base/window" |
| 8 |
], function(declare, connect, html, lang, event, win){ |
| 9 |
|
| 10 |
return declare("dojox.geo.openlayers.TouchInteractionSupport", null, { |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
_map: null, |
| 17 |
_centerTouchLocation: null, |
| 18 |
_touchMoveListener: null, |
| 19 |
_touchEndListener: null, |
| 20 |
_initialFingerSpacing: null, |
| 21 |
_initialScale: null, |
| 22 |
_tapCount: null, |
| 23 |
_tapThreshold: null, |
| 24 |
_lastTap: null, |
| 25 |
|
| 26 |
constructor: function(map){ |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
this._map = map; |
| 32 |
this._centerTouchLocation = new OpenLayers.LonLat(0, 0); |
| 33 |
|
| 34 |
var div = this._map.div; |
| 35 |
|
| 36 |
|
| 37 |
connect.connect(div, "touchstart", this, this._touchStartHandler); |
| 38 |
connect.connect(div, "touchmove", this, this._touchMoveHandler); |
| 39 |
connect.connect(div, "touchend", this, this._touchEndHandler); |
| 40 |
|
| 41 |
this._tapCount = 0; |
| 42 |
this._lastTap = { |
| 43 |
x: 0, |
| 44 |
y: 0 |
| 45 |
}; |
| 46 |
this._tapThreshold = 100; |
| 47 |
|
| 48 |
}, |
| 49 |
|
| 50 |
_getTouchBarycenter: function(touchEvent){ |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
var touches = touchEvent.touches; |
| 60 |
var firstTouch = touches[0]; |
| 61 |
var secondTouch = null; |
| 62 |
if(touches.length > 1){ |
| 63 |
secondTouch = touches[1]; |
| 64 |
}else{ |
| 65 |
secondTouch = touches[0]; |
| 66 |
} |
| 67 |
|
| 68 |
var marginBox = html.marginBox(this._map.div); |
| 69 |
|
| 70 |
var middleX = (firstTouch.pageX + secondTouch.pageX) / 2.0 - marginBox.l; |
| 71 |
var middleY = (firstTouch.pageY + secondTouch.pageY) / 2.0 - marginBox.t; |
| 72 |
|
| 73 |
return { |
| 74 |
x: middleX, |
| 75 |
y: middleY |
| 76 |
}; |
| 77 |
|
| 78 |
}, |
| 79 |
|
| 80 |
_getFingerSpacing: function(touchEvent){ |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
var touches = touchEvent.touches; |
| 90 |
var spacing = -1; |
| 91 |
if(touches.length >= 2){ |
| 92 |
var dx = (touches[1].pageX - touches[0].pageX); |
| 93 |
var dy = (touches[1].pageY - touches[0].pageY); |
| 94 |
spacing = Math.sqrt(dx * dx + dy * dy); |
| 95 |
} |
| 96 |
return spacing; |
| 97 |
}, |
| 98 |
|
| 99 |
_isDoubleTap: function(touchEvent){ |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
var isDoubleTap = false; |
| 110 |
var touches = touchEvent.touches; |
| 111 |
if((this._tapCount > 0) && touches.length == 1){ |
| 112 |
|
| 113 |
var dx = (touches[0].pageX - this._lastTap.x); |
| 114 |
var dy = (touches[0].pageY - this._lastTap.y); |
| 115 |
var distance = dx * dx + dy * dy; |
| 116 |
if(distance < this._tapThreshold){ |
| 117 |
isDoubleTap = true; |
| 118 |
}else{ |
| 119 |
this._tapCount = 0; |
| 120 |
} |
| 121 |
} |
| 122 |
this._tapCount++; |
| 123 |
this._lastTap.x = touches[0].pageX; |
| 124 |
this._lastTap.y = touches[0].pageY; |
| 125 |
setTimeout(lang.hitch(this, function(){ |
| 126 |
this._tapCount = 0; |
| 127 |
}), 300); |
| 128 |
|
| 129 |
return isDoubleTap; |
| 130 |
}, |
| 131 |
|
| 132 |
_doubleTapHandler: function(touchEvent){ |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
var touches = touchEvent.touches; |
| 142 |
var marginBox = html.marginBox(this._map.div); |
| 143 |
var offX = touches[0].pageX - marginBox.l; |
| 144 |
var offY = touches[0].pageY - marginBox.t; |
| 145 |
|
| 146 |
var mapPoint = this._map.getLonLatFromPixel(new OpenLayers.Pixel(offX, offY)); |
| 147 |
|
| 148 |
this._map.setCenter(new OpenLayers.LonLat(mapPoint.lon, mapPoint.lat), this._map.getZoom() + 1); |
| 149 |
}, |
| 150 |
|
| 151 |
_touchStartHandler: function(touchEvent){ |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
event.stop(touchEvent); |
| 159 |
|
| 160 |
|
| 161 |
if(this._isDoubleTap(touchEvent)){ |
| 162 |
this._doubleTapHandler(touchEvent); |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
var middlePoint = this._getTouchBarycenter(touchEvent); |
| 168 |
|
| 169 |
this._centerTouchLocation = this._map.getLonLatFromPixel(new OpenLayers.Pixel(middlePoint.x, middlePoint.y)); |
| 170 |
|
| 171 |
|
| 172 |
this._initialFingerSpacing = this._getFingerSpacing(touchEvent); |
| 173 |
|
| 174 |
|
| 175 |
this._initialScale = this._map.getScale(); |
| 176 |
|
| 177 |
|
| 178 |
if(!this._touchMoveListener){ |
| 179 |
this._touchMoveListener = connect.connect(win.global, "touchmove", this, this._touchMoveHandler); |
| 180 |
} |
| 181 |
if(!this._touchEndListener){ |
| 182 |
this._touchEndListener = connect.connect(win.global, "touchend", this, this._touchEndHandler); |
| 183 |
} |
| 184 |
}, |
| 185 |
|
| 186 |
_touchEndHandler: function(touchEvent){ |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
event.stop(touchEvent); |
| 194 |
|
| 195 |
var touches = touchEvent.touches; |
| 196 |
|
| 197 |
if(touches.length == 0){ |
| 198 |
|
| 199 |
if(this._touchMoveListener){ |
| 200 |
connect.disconnect(this._touchMoveListener); |
| 201 |
this._touchMoveListener = null; |
| 202 |
} |
| 203 |
if(this._touchEndListener){ |
| 204 |
connect.disconnect(this._touchEndListener); |
| 205 |
this._touchEndListener = null; |
| 206 |
} |
| 207 |
}else{ |
| 208 |
|
| 209 |
var middlePoint = this._getTouchBarycenter(touchEvent); |
| 210 |
|
| 211 |
this._centerTouchLocation = this._map.getLonLatFromPixel(new OpenLayers.Pixel(middlePoint.x, middlePoint.y)); |
| 212 |
} |
| 213 |
}, |
| 214 |
|
| 215 |
_touchMoveHandler: function(touchEvent){ |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
event.stop(touchEvent); |
| 225 |
|
| 226 |
var middlePoint = this._getTouchBarycenter(touchEvent); |
| 227 |
|
| 228 |
|
| 229 |
var mapPoint = this._map.getLonLatFromPixel(new OpenLayers.Pixel(middlePoint.x, middlePoint.y)); |
| 230 |
var mapOffsetLon = mapPoint.lon - this._centerTouchLocation.lon; |
| 231 |
var mapOffsetLat = mapPoint.lat - this._centerTouchLocation.lat; |
| 232 |
|
| 233 |
|
| 234 |
var scaleFactor = 1; |
| 235 |
var touches = touchEvent.touches; |
| 236 |
if(touches.length >= 2){ |
| 237 |
var fingerSpacing = this._getFingerSpacing(touchEvent); |
| 238 |
scaleFactor = fingerSpacing / this._initialFingerSpacing; |
| 239 |
|
| 240 |
this._map.zoomToScale(this._initialScale / scaleFactor); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
var currentMapCenter = this._map.getCenter(); |
| 245 |
this._map.setCenter(new OpenLayers.LonLat(currentMapCenter.lon - mapOffsetLon, currentMapCenter.lat |
| 246 |
- mapOffsetLat)); |
| 247 |
|
| 248 |
} |
| 249 |
}); |
| 250 |
}); |