# all files / ol/interaction/ dragrotateandzoominteraction.js

34.48% Statements 20/58
15% Branches 3/20
25% Functions 1/4
34.48% Lines 20/58

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    1531×

    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

     goog.provide('ol.interaction.DragRotateAndZoom');

    goog.require('goog.math.Vec2');
    goog.require('ol');
    goog.require('ol.ViewHint');
    goog.require('ol.events.ConditionType');
    goog.require('ol.events.condition');
    goog.require('ol.interaction.Interaction');
    goog.require('ol.interaction.Pointer');

    /**
     * @classdesc
     * Allows the user to zoom and rotate the map by clicking and dragging
     * on the map.  By default, this interaction is limited to when the shift
     * key is held down.
     *
     * This interaction is only supported for mouse devices.
     *
     * And this interaction is not included in the default interactions.
     *
     * @constructor
     * @extends {ol.interaction.Pointer}
     * @param {olx.interaction.DragRotateAndZoomOptions=} opt_options Options.
     * @api stable
     */
    ol.interaction.DragRotateAndZoom = function(opt_options) {

      var options = opt_options ? opt_options : {};

      goog.base(this, {
        handleDownEvent: ol.interaction.DragRotateAndZoom.handleDownEvent_,
        handleDragEvent: ol.interaction.DragRotateAndZoom.handleDragEvent_,
        handleUpEvent: ol.interaction.DragRotateAndZoom.handleUpEvent_
      });

      /**
       * @private
       * @type {ol.events.ConditionType}
       */
      this.condition_ = options.condition ?
          options.condition : ol.events.condition.shiftKeyOnly;

      /**
       * @private
       * @type {number|undefined}
       */
      this.lastAngle_ = undefined;

      /**
       * @private
       * @type {number|undefined}
       */
      this.lastMagnitude_ = undefined;

      /**
       * @private
       * @type {number}
       */
      this.lastScaleDelta_ = 0;

      /**
       * @private
       * @type {number}
       */
      this.duration_ = options.duration ? options.duration : 400;

    };
    goog.inherits(ol.interaction.DragRotateAndZoom, ol.interaction.Pointer);

    /**
     * @param {ol.MapBrowserPointerEvent} mapBrowserEvent Event.
     * @this {ol.interaction.DragRotateAndZoom}
     * @private
     */
    ol.interaction.DragRotateAndZoom.handleDragEvent_ = function(mapBrowserEvent) {
      if (!ol.events.condition.mouseOnly(mapBrowserEvent)) {
        return;
      }

      var map = mapBrowserEvent.map;
      var size = map.getSize();
      var offset = mapBrowserEvent.pixel;
      var delta = new goog.math.Vec2(
          offset[0] - size[0] / 2,
          size[1] / 2 - offset[1]);
      var theta = Math.atan2(delta.y, delta.x);
      var magnitude = delta.magnitude();
      var view = map.getView();
      map.render();
      if (this.lastAngle_ !== undefined) {
        var angleDelta = theta - this.lastAngle_;
        ol.interaction.Interaction.rotateWithoutConstraints(
            map, view, view.getRotation() - angleDelta);
      }
      this.lastAngle_ = theta;
      if (this.lastMagnitude_ !== undefined) {
        var resolution = this.lastMagnitude_ * (view.getResolution() / magnitude);
        ol.interaction.Interaction.zoomWithoutConstraints(map, view, resolution);
      }
      if (this.lastMagnitude_ !== undefined) {
        this.lastScaleDelta_ = this.lastMagnitude_ / magnitude;
      }
      this.lastMagnitude_ = magnitude;
    };

    /**
     * @param {ol.MapBrowserPointerEvent} mapBrowserEvent Event.
     * @return {boolean} Stop drag sequence?
     * @this {ol.interaction.DragRotateAndZoom}
     * @private
     */
    ol.interaction.DragRotateAndZoom.handleUpEvent_ = function(mapBrowserEvent) {
      if (!ol.events.condition.mouseOnly(mapBrowserEvent)) {
        return true;
      }

      var map = mapBrowserEvent.map;
      var view = map.getView();
      view.setHint(ol.ViewHint.INTERACTING, -1);
      var direction = this.lastScaleDelta_ - 1;
      ol.interaction.Interaction.rotate(map, view, view.getRotation());
      ol.interaction.Interaction.zoom(map, view, view.getResolution(),
          undefined, this.duration_, direction);
      this.lastScaleDelta_ = 0;
      return false;
    };

    /**
     * @param {ol.MapBrowserPointerEvent} mapBrowserEvent Event.
     * @return {boolean} Start drag sequence?
     * @this {ol.interaction.DragRotateAndZoom}
     * @private
     */
    ol.interaction.DragRotateAndZoom.handleDownEvent_ = function(mapBrowserEvent) {
      if (!ol.events.condition.mouseOnly(mapBrowserEvent)) {
        return false;
      }

      if (this.condition_(mapBrowserEvent)) {
        mapBrowserEvent.map.getView().setHint(ol.ViewHint.INTERACTING, 1);
        this.lastAngle_ = undefined;
        this.lastMagnitude_ = undefined;
        return true;
      } else {
        return false;
      }
    };