Statistics
| Revision:

root / i3geo / pacotes / google / keydragzoom.js.php @ 3260

History | View | Annotate | Download (20.7 KB)

1
<?php error_reporting(0);if(extension_loaded('zlib')){ob_start('ob_gzhandler');} header("Content-type: text/javascript"); ?>
2
/**
3
 * @name Key Drag Zoom for V3
4
 * @version 1.0
5
 * @author: Nianwei Liu [nianwei at gmail dot com] & Gary Little [gary at luxcentral dot com]
6
 * @fileoverview This library adds a drag zoom capability to a Google map.
7
 *  When drag zoom is enabled, holding down a user-defined hot key <code>(shift | ctrl | alt)</code>
8
 *  while dragging a box around an area of interest will zoom the map
9
 *  to that area when the hot key is released.
10
 *  Only one line of code is needed: <code>google.maps.Map.enableKeyDragZoom();</code>
11
 *  <p>
12
 *  Note that if the map's container has a border around it, the border widths must be specified
13
 *  in pixel units (or as thin, medium, or thick). This is required because of an MSIE limitation.
14
 *  <p>NL: 2009-05-28: initial port to core API V3.
15
 */
16
/*!
17
 *
18
 * Licensed under the Apache License, Version 2.0 (the "License");
19
 * you may not use this file except in compliance with the License.
20
 * You may obtain a copy of the License at
21
 *
22
 *       http://www.apache.org/licenses/LICENSE-2.0
23
 *
24
 * Unless required by applicable law or agreed to in writing, software
25
 * distributed under the License is distributed on an "AS IS" BASIS,
26
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27
 * See the License for the specific language governing permissions and
28
 * limitations under the License.
29
 */
30
(function () {
31
  /*jslint browser:true */
32
   /*global google */
33
  /**
34
   * Converts 'thin', 'medium', and 'thick' to pixel widths
35
   * in an MSIE environment. Not called for other browsers
36
   * because getComputedStyle() returns pixel widths automatically.
37
   * @param {String} widthValue
38
   */
39
  var toPixels = function (widthValue) {
40
    var px;
41
    switch (widthValue) {
42
    case 'thin':
43
      px = "2px";
44
      break;
45
    case 'medium':
46
      px = "4px";
47
      break;
48
    case 'thick':
49
      px = "6px";
50
      break;
51
    default:
52
      px = widthValue;
53
    }
54
    return px;
55
  };
56
 /**
57
  * Get the widths of the borders of an HTML element.
58
  *
59
  * @param {Object} h  HTML element
60
  * @return {Object} widths object (top, bottom left, right)
61
  */
62
  var getBorderWidths = function (h) {
63
    var computedStyle;
64
    var bw = {};
65
    if (document.defaultView && document.defaultView.getComputedStyle) {
66
      computedStyle = h.ownerDocument.defaultView.getComputedStyle(h, "");
67
      if (computedStyle) {
68
        // The computed styles are always in pixel units (good!)
69
        bw.top = parseInt(computedStyle.borderTopWidth, 10) || 0;
70
        bw.bottom = parseInt(computedStyle.borderBottomWidth, 10) || 0;
71
        bw.left = parseInt(computedStyle.borderLeftWidth, 10) || 0;
72
        bw.right = parseInt(computedStyle.borderRightWidth, 10) || 0;
73
        return bw;
74
      }
75
    } else if (document.documentElement.currentStyle) { // MSIE
76
      if (h.currentStyle) {
77
        // The current styles may not be in pixel units so try to convert (bad!)
78
        bw.top = parseInt(toPixels(h.currentStyle.borderTopWidth), 10) || 0;
79
        bw.bottom = parseInt(toPixels(h.currentStyle.borderBottomWidth), 10) || 0;
80
        bw.left = parseInt(toPixels(h.currentStyle.borderLeftWidth), 10) || 0;
81
        bw.right = parseInt(toPixels(h.currentStyle.borderRightWidth), 10) || 0;
82
        return bw;
83
      }
84
    }
85
    // Shouldn't get this far for any modern browser
86
    bw.top = parseInt(h.style["border-top-width"], 10) || 0;
87
    bw.bottom = parseInt(h.style["border-bottom-width"], 10) || 0;
88
    bw.left = parseInt(h.style["border-left-width"], 10) || 0;
89
    bw.right = parseInt(h.style["border-right-width"], 10) || 0;
90
    return bw;
91
  };
92

    
93
  /**
94
   * Get the position of the mouse relative to the document.
95
   * @param {Object} e  Mouse event
96
   * @return {Object} left & top position
97
   */
98
  var getMousePosition = function (e) {
99
    var posX = 0, posY = 0;
100
    e = e || window.event;
101
    if (typeof e.pageX !== "undefined") {
102
      posX = e.pageX;
103
      posY = e.pageY;
104
    } else if (typeof e.clientX !== "undefined") {
105
      posX = e.clientX +
106
      (typeof document.documentElement.scrollLeft !== "undefined" ? document.documentElement.scrollLeft : document.body.scrollLeft);
107
      posY = e.clientY +
108
      (typeof document.documentElement.scrollTop !== "undefined" ? document.documentElement.scrollTop : document.body.scrollTop);
109
    }
110
    return {
111
      left: posX,
112
      top: posY
113
    };
114
  };
115

    
116
  /**
117
   * Get the position of an HTML element relative to the document.
118
   * @param {Object} h  HTML element
119
   * @return {Object} left & top position
120
   */
121
  var getElementPosition = function (h) {
122
    var posX = h.offsetLeft;
123
    var posY = h.offsetTop;
124
    var parent = h.offsetParent;
125
    // Add offsets for all ancestors in the hierarchy
126
    while (parent !== null) {
127
      // Adjust for scrolling elements which may affect the map position.
128
      //
129
      // See http://www.howtocreate.co.uk/tutorials/javascript/browserspecific
130
      //
131
      // "...make sure that every element [on a Web page] with an overflow
132
      // of anything other than visible also has a position style set to
133
      // something other than the default static..."
134
      if (parent !== document.body && parent !== document.documentElement) {
135
        posX -= parent.scrollLeft;
136
        posY -= parent.scrollTop;
137
      }
138
      posX += parent.offsetLeft;
139
      posY += parent.offsetTop;
140
      parent = parent.offsetParent;
141
    }
142
    return {
143
      left: posX,
144
      top: posY
145
    };
146
  };
147
  /**
148
   * Set the properties of an object to those from another object.
149
   * @param {Object} obj target object
150
   * @param {Object} vals source object
151
   */
152
  var setVals = function (obj, vals) {
153
    if (obj && vals) {
154
      for (var x in vals) {
155
        if (vals.hasOwnProperty(x)) {
156
          obj[x] = vals[x];
157
        }
158
      }
159
    }
160
    return obj;
161
  };
162
  /**
163
   * Set the opacity. If op is not passed in, this function just performs an MSIE fix.
164
   * @param {Node} div
165
   * @param {Number} op (0-1)
166
   */
167
  var setOpacity = function (div, op) {
168
    if (typeof op !== 'undefined') {
169
      div.style.opacity = op;
170
    }
171
    if (typeof div.style.opacity !== 'undefined') {
172
      div.style.filter = "alpha(opacity=" + (div.style.opacity * 100) + ")";
173
    }
174
  };
175

    
176
  /**
177
   * @name KeyDragZoomOptions
178
   * @class This class represents the optional parameter passed into <code>google.maps.Map.enableDragBoxZoom</code>.
179
   * @property {String} [key] the hot key to hold down to activate a drag zoom, <code>shift | ctrl | alt</code>.
180
   * The default is <code>shift</code>.
181
   * @property {Object} [boxStyle] the css style of the zoom box.
182
   * The default is <code>{border: 'thin solid #FF0000'}</code>.
183
   * Border widths must be specified in pixel units (or as thin, medium, or thick).
184
   * @property {Object} [paneStyle] the css style of the pane which overlays the map when a drag zoom is activated.
185
   * The default is <code>{backgroundColor: 'white', opacity: 0.0, cursor: 'crosshair'}</code>.
186
   */
187
  /**
188
   * @name DragZoom
189
   * @class This class represents a drag zoom object for a map. The object is activated by holding down the hot key.
190
   * This object is created when <code>google.maps.Map.enableKeyDragZoom</code> is called; it cannot be created directly.
191
   * Use <code>google.maps.Map.getDragZoomObject</code> to gain access to this object in order to attach event listeners.
192
   * @param {google.maps.Map} map
193
   * @param {KeyDragZoomOptions} opt_zoomOpts
194
   */
195
  function DragZoom(map, opt_zoomOpts) {
196
    var ov = new google.maps.OverlayView();
197
    var me = this;
198
    ov.onAdd = function () {
199
      me.init_(map, opt_zoomOpts);
200
    };
201
    ov.draw = function () {
202
    };
203
    ov.onRemove = function () {
204
    };
205
    ov.setMap(map);
206
    this.prjov_ = ov;
207
  }
208
  /**
209
   * Init the tool.
210
   * @param {google.maps.Map} map
211
   * @param {KeyDragZoomOptions} opt_zoomOpts
212
   */
213
  DragZoom.prototype.init_ = function (map, opt_zoomOpts) {
214
    this.map_ = map;
215
    opt_zoomOpts = opt_zoomOpts || {};
216
    this.key_ = opt_zoomOpts.key || 'shift';
217
    this.key_ = this.key_.toLowerCase();
218
    this.borderWidths_ = getBorderWidths(this.map_.getDiv());//Container());
219
    this.paneDiv_ = document.createElement("div");
220
    this.paneDiv_.onselectstart = function () {
221
      return false;
222
    };
223
    // default style
224
    setVals(this.paneDiv_.style, {
225
      backgroundColor: 'white',
226
      opacity: 0.0,
227
      cursor: 'crosshair'
228
    });
229
    // allow overwrite
230
    setVals(this.paneDiv_.style, opt_zoomOpts.paneStyle);
231
    // stuff that cannot be overwritten
232
    setVals(this.paneDiv_.style, {
233
      position: 'absolute',
234
      overflow: 'hidden',
235
      zIndex: 10001,
236
      display: 'none'
237
    });
238
    if (this.key_ === 'shift') { // Workaround for Firefox Shift-Click problem
239
      this.paneDiv_.style.MozUserSelect = "none";
240
    }
241
    setOpacity(this.paneDiv_);
242
    // An IE fix: if the background is transparent, it cannot capture mousedown events
243
    if (this.paneDiv_.style.backgroundColor === 'transparent') {
244
      this.paneDiv_.style.backgroundColor = 'white';
245
      setOpacity(this.paneDiv_, 0);
246
    }
247
    this.map_.getDiv().appendChild(this.paneDiv_);//Container()
248
    this.boxDiv_ = document.createElement('div');
249
    setVals(this.boxDiv_.style, {
250
      border: 'thin solid #FF0000'
251
    });
252
    setVals(this.boxDiv_.style, opt_zoomOpts.boxStyle);
253
    setVals(this.boxDiv_.style, {
254
      position: 'absolute',
255
      display: 'none'
256
    });
257
    setOpacity(this.boxDiv_);
258
    this.map_.getDiv().appendChild(this.boxDiv_);
259
    this.boxBorderWidths_ = getBorderWidths(this.boxDiv_);
260
    var me = this;
261
    this.keyDownListener_ = google.maps.event.addDomListener(document, 'keydown',  function (e) {
262
      me.onKeyDown_(e);
263
    });
264
    this.keyUpListener_ = google.maps.event.addDomListener(document, 'keyup', function (e) {
265
      me.onKeyUp_(e);
266
    });
267
    this.mouseDownListener_ = google.maps.event.addDomListener(this.paneDiv_, 'mousedown', function (e) {
268
      me.onMouseDown_(e);
269
    });
270
    this.mouseDownListenerDocument_ = google.maps.event.addDomListener(document, 'mousedown', function (e) {
271
      me.onMouseDownDocument_(e);
272
    });
273
    this.mouseMoveListener_ = google.maps.event.addDomListener(document, 'mousemove', function (e) {
274
      me.onMouseMove_(e);
275
    });
276
    this.mouseUpListener_ = google.maps.event.addDomListener(document, 'mouseup', function (e) {
277
      me.onMouseUp_(e);
278
    });
279

    
280
    this.hotKeyDown_ = false;
281
    this.dragging_ = false;
282
    this.startPt_ = null;
283
    this.endPt_ = null;
284
    this.boxMaxX_ = null;
285
    this.boxMaxY_ = null;
286
    this.mousePosn_ = null;
287
    this.mapPosn_ = getElementPosition(this.map_.getDiv());
288
    this.mouseDown_ = false;
289
  };
290

    
291
  /**
292
   * Returns true if the hot key is being pressed when an event occurs.
293
   * @param {Event} e
294
   * @return {Boolean}
295
   */
296
  DragZoom.prototype.isHotKeyDown_ = function (e) {
297
    var isHot;
298
    e = e || window.event;
299
    isHot = (e.shiftKey && this.key_ === 'shift') || (e.altKey && this.key_ === 'alt') || (e.ctrlKey && this.key_ === 'ctrl');
300
    if (!isHot) {
301
      // Need to look at keyCode for Opera because it
302
      // doesn't set the shiftKey, altKey, ctrlKey properties
303
      // unless a non-modifier event is being reported.
304
      //
305
      // See http://cross-browser.com/x/examples/shift_mode.php
306
      // Also see http://unixpapa.com/js/key.html
307
      switch (e.keyCode) {
308
      case 16:
309
        if (this.key_ === 'shift') {
310
          isHot = true;
311
        }
312
        break;
313
      case 17:
314
        if (this.key_ === 'ctrl') {
315
          isHot = true;
316
        }
317
        break;
318
      case 18:
319
        if (this.key_ === 'alt') {
320
          isHot = true;
321
        }
322
        break;
323
      }
324
    }
325
    return isHot;
326
  };
327

    
328
  /**
329
   * Checks if the mouse is on top of the map. The position is captured
330
   * in onMouseMove_.
331
   * @return true if mouse is on top of the map div.
332
   */
333
  DragZoom.prototype.isMouseOnMap_ = function () {
334
    var mousePos = this.mousePosn_;
335
    if (mousePos) {
336
      var mapPos = this.mapPosn_;
337
      var mapDiv = this.map_.getDiv();
338
      return mousePos.left > mapPos.left && mousePos.left < mapPos.left + mapDiv.offsetWidth &&
339
      mousePos.top > mapPos.top && mousePos.top < mapPos.top + mapDiv.offsetHeight;
340
    } else {
341
      // if user never moved mouse
342
      return false;
343
    }
344
  };
345

    
346
  /**
347
   * Show or hide the overlay pane, depending on whether the mouse is over the map.
348
   */
349
  DragZoom.prototype.setPaneVisibility_ = function () {
350
    if (this.map_ && this.hotKeyDown_ && this.isMouseOnMap_()) {
351
      var mapDiv = this.map_.getDiv();
352
      this.paneDiv_.style.left = 0 + 'px';
353
      this.paneDiv_.style.top = 0 + 'px';
354
      this.paneDiv_.style.width = mapDiv.offsetWidth - (this.borderWidths_.left + this.borderWidths_.right) + 'px';
355
      this.paneDiv_.style.height = mapDiv.offsetHeight - (this.borderWidths_.top + this.borderWidths_.bottom) + 'px';
356
      this.paneDiv_.style.display = 'block';
357
      this.boxMaxX_ = parseInt(this.paneDiv_.style.width, 10) - (this.boxBorderWidths_.left + this.boxBorderWidths_.right);
358
      this.boxMaxY_ = parseInt(this.paneDiv_.style.height, 10) - (this.boxBorderWidths_.top + this.boxBorderWidths_.bottom);
359
    } else {
360
      this.paneDiv_.style.display = 'none';
361
    }
362
  };
363
  /**
364
   * Handle key down. Activate the tool only if the mouse is on top of the map.
365
   * @param {Event} e
366
   */
367
  DragZoom.prototype.onKeyDown_ = function (e) {
368
    var me = this;
369
    if (this.map_ && !this.hotKeyDown_ && this.isHotKeyDown_(e)) {
370
      //desativa o clique permanente
371
      i3GEO.eventos.cliquePerm.desativa();
372
      me.hotKeyDown_ = true;
373
      me.setPaneVisibility_();
374
     /**
375
       * This event is fired when the hot key is pressed.
376
       * @name DragZoom#activate
377
       * @event
378
       */
379
      google.maps.event.trigger(me, 'activate');
380
    }
381
  };
382
  /**
383
   * Get the <code>google.maps.Point</code> of the mouse position.
384
   * @param {Object} e
385
   * @return {google.maps.Point} point
386
   * @private
387
   */
388
  DragZoom.prototype.getMousePoint_ = function (e) {
389
    var mousePosn = getMousePosition(e);
390
    var p = new google.maps.Point();
391
    p.x = mousePosn.left - this.mapPosn_.left - this.borderWidths_.left;
392
    p.y = mousePosn.top - this.mapPosn_.top - this.borderWidths_.top;
393
    p.x = Math.min(p.x, this.boxMaxX_);
394
    p.y = Math.min(p.y, this.boxMaxY_);
395
    p.x = Math.max(p.x, 0);
396
    p.y = Math.max(p.y, 0);
397
    return p;
398
  };
399
  /**
400
   * Handle mouse down.
401
   * @param {Event} e
402
   */
403
  DragZoom.prototype.onMouseDown_ = function (e) {
404
    if (this.map_ && this.hotKeyDown_) {
405
      this.mapPosn_ = getElementPosition(this.map_.getDiv());
406
      this.dragging_ = true;
407
      this.startPt_ = this.endPt_ = this.getMousePoint_(e);
408
      var prj = this.prjov_.getProjection();
409
      var latlng = prj.fromDivPixelToLatLng(this.startPt_);
410
      /**
411
       * This event is fired when the drag operation begins.
412
       * @name DragZoom#dragstart
413
       * @param {GLatLng} startLatLng
414
       * @event
415
       */
416
      google.maps.event.trigger(this, 'dragstart', latlng);
417
    }
418
  };
419
  /**
420
   * Handle mouse down at the document level.
421
   * @param {Event} e
422
   */
423
  DragZoom.prototype.onMouseDownDocument_ = function (e) {
424
    this.mouseDown_ = true;
425
  };
426
  /**
427
   * Handle mouse move.
428
   * @param {Event} e
429
   */
430
  DragZoom.prototype.onMouseMove_ = function (e) {
431
    this.mousePosn_ = getMousePosition(e);
432
    if (this.dragging_) {
433
      this.endPt_ = this.getMousePoint_(e);
434
      var left = Math.min(this.startPt_.x, this.endPt_.x);
435
      var top = Math.min(this.startPt_.y, this.endPt_.y);
436
      var width = Math.abs(this.startPt_.x - this.endPt_.x);
437
      var height = Math.abs(this.startPt_.y - this.endPt_.y);
438
      this.boxDiv_.style.left = left + 'px';
439
      this.boxDiv_.style.top = top + 'px';
440
      this.boxDiv_.style.width = width + 'px';
441
      this.boxDiv_.style.height = height + 'px';
442
      this.boxDiv_.style.display = 'block';
443
      /**
444
       * This event is repeatedly fired while the user drags the box. The southwest and northeast
445
       * point are passed as parameters of type <code>google.maps.Point</code> (for performance reasons),
446
       * relative to the map container. Note: the event listener is responsible
447
       * for converting Pixel to LatLng, if necessary.
448
       * @name DragZoom#drag
449
       * @param {google.maps.Point} southwestPixel
450
       * @param {google.maps.Point} northeastPixel
451
       * @event
452
       */
453
      google.maps.event.trigger(this, 'drag', new google.maps.Point(left, top + height), new google.maps.Point(left + width, top));
454
    } else if (!this.mouseDown_) {
455
      this.setPaneVisibility_();
456
    }
457
  };
458
  /**
459
   * Handle mouse up.
460
   * @param {Event} e
461
   */
462
  DragZoom.prototype.onMouseUp_ = function (e) {
463
    this.mouseDown_ = false;
464
    if (this.dragging_) {
465
      //desativa o clique permanente
466
      i3GEO.eventos.cliquePerm.ativa();
467
      var left = Math.min(this.startPt_.x, this.endPt_.x);
468
      var top = Math.min(this.startPt_.y, this.endPt_.y);
469
      var width = Math.abs(this.startPt_.x - this.endPt_.x);
470
      var height = Math.abs(this.startPt_.y - this.endPt_.y);
471
      var prj = this.prjov_.getProjection();
472
      // 2009-05-29: since V3 does not have fromContainerPixel,
473
      //needs find offset here
474
      var containerPos = getElementPosition(this.map_.getDiv());
475
      var mapPanePos = getElementPosition(this.prjov_.getPanes().mapPane);
476
      left = left + (containerPos.left - mapPanePos.left);
477
      top = top + (containerPos.top - mapPanePos.top);
478
      var sw = prj.fromDivPixelToLatLng(new google.maps.Point(left, top + height));
479
      var ne = prj.fromDivPixelToLatLng(new google.maps.Point(left + width, top));
480
      var bnds = new google.maps.LatLngBounds(sw, ne);
481
      this.map_.fitBounds(bnds);
482
      this.dragging_ = false;
483
      this.boxDiv_.style.display = 'none';
484
      /**
485
       * This event is fired when the drag operation ends.
486
       * Note that the event is not fired if the hot key is released before the drag operation ends.
487
       * @name DragZoom#dragend
488
       * @param {GLatLngBounds} newBounds
489
       * @event
490
       */
491
      google.maps.event.trigger(this, 'dragend', bnds);
492
    }
493
  };
494

    
495
  /**
496
   * Handle key up.
497
   * @param {Event} e
498
   */
499
  DragZoom.prototype.onKeyUp_ = function (e) {
500
    if (this.map_ && this.hotKeyDown_) {
501
      this.hotKeyDown_ = false;
502
      this.dragging_ = false;
503
      this.boxDiv_.style.display = 'none';
504
      this.paneDiv_.style.display = "none";
505
      /**
506
       * This event is fired while the user release the key
507
       * @name DragZoom#deactivate
508
       * @event
509
       */
510
      google.maps.event.trigger(this, 'deactivate');
511
    }
512
  };
513

    
514

    
515

    
516

    
517
  /**
518
   * @name google.maps.Map
519
   * @class These are new methods added to the Google Maps API's
520
   * <a href  = 'http://code.google.com/apis/maps/documentation/v3/reference.html#Map'>Map</a>
521
   * class.
522
   */
523
  /**
524
   * Enable drag zoom. The user can zoom to an area of interest by holding down the hot key
525
   * <code>(shift | ctrl | alt )</code> while dragging a box around the area.
526
   * @param {KeyDragZoomOptions} opt_zoomOpts
527
   */
528

    
529
  google.maps.Map.prototype.enableKeyDragZoom = function (opt_zoomOpts) {
530
    this.dragZoom_ = new DragZoom(this, opt_zoomOpts);
531
  };
532
  /**
533
   * Disable drag zoom.
534
   */
535
  google.maps.Map.prototype.disableKeyDragZoom = function () {
536
    var d = this.dragZoom_;
537
    if (d) {
538
      google.maps.event.removeListener(d.mouseDownListener_);
539
      google.maps.event.removeListener(d.mouseDownListenerDocument_);
540
      google.maps.event.removeListener(d.mouseMoveListener_);
541
      google.maps.event.removeListener(d.mouseUpListener_);
542
      google.maps.event.removeListener(d.keyUpListener_);
543
      google.maps.event.removeListener(d.keyDownListener_);
544
      this.getDiv().removeChild(d.boxDiv_);
545
      this.getDiv().removeChild(d.paneDiv_);
546
      this.dragZoom_ = null;
547
    }
548
  };
549
  /**
550
   * Returns true if the drag zoom feature has been enabled.
551
   * @return {Boolean}
552
   */
553
  google.maps.Map.prototype.keyDragZoomEnabled = function () {
554
    return this.dragZoom_ !== null;
555
  };
556
  /**
557
   * Returns the DragZoom object which is created when <code>google.maps.Map.enableKeyDragZoom</code> is called.
558
   * With this object you can use <code>google.maps.event.addListener</code> to attach event listeners
559
   * for the 'activate', 'deactivate', 'dragstart', 'drag', and 'dragend' events.
560
   * @return {DragZoom}
561
   */
562
  google.maps.Map.prototype.getDragZoomObject = function () {
563
    return this.dragZoom_;
564
  };
565
})();
566
<?php error_reporting(0);if(extension_loaded('zlib')){ob_end_flush();}?>