~dongpo-deng/sahana-eden/test

« back to all changes in this revision

Viewing changes to static/scripts/gis/MP.js

  • Committer: Deng Dongpo
  • Date: 2010-08-01 09:29:44 UTC
  • Revision ID: dongpo@dhcp-21193.iis.sinica.edu.tw-20100801092944-8t9obt4xtl7otesb
initial

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
 
2
 * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the
 
3
 * full text of the license. */
 
4
 
 
5
 
 
6
/**
 
7
 * @requires OpenLayers/Control.js
 
8
 */
 
9
 
 
10
/**
 
11
 * Class: OpenLayers.Control.MousePosition
 
12
 * The MousePosition control displays geographic coordinates of the mouse
 
13
 * pointer, as it is moved about the map.
 
14
 *
 
15
 * Inherits from:
 
16
 *  - <OpenLayers.Control>
 
17
 */
 
18
OpenLayers.Control.MGRSMousePosition = OpenLayers.Class(OpenLayers.Control, {
 
19
 
 
20
    /**
 
21
     * Property: element
 
22
     * {DOMElement}
 
23
     */
 
24
    element: null,
 
25
 
 
26
    /**
 
27
     * APIProperty: prefix
 
28
     * {String}
 
29
     */
 
30
    prefix: '',
 
31
 
 
32
    /**
 
33
     * APIProperty: separator
 
34
     * {String}
 
35
     */
 
36
    separator: ', ',
 
37
 
 
38
    /**
 
39
     * APIProperty: suffix
 
40
     * {String}
 
41
     */
 
42
    suffix: '',
 
43
 
 
44
    /**
 
45
     * APIProperty: numDigits
 
46
     * {Integer}
 
47
     */
 
48
    numDigits: 5,
 
49
 
 
50
    /**
 
51
     * APIProperty: granularity
 
52
     * {Integer}
 
53
     */
 
54
    granularity: 10,
 
55
 
 
56
    /**
 
57
     * Property: lastXy
 
58
     * {<OpenLayers.Pixel>}
 
59
     */
 
60
    lastXy: null,
 
61
 
 
62
    /**
 
63
     * APIProperty: displayProjection
 
64
     * {<OpenLayers.Projection>} The projection in which the
 
65
     * mouse position is displayed
 
66
     */
 
67
    displayProjection: null,
 
68
 
 
69
    /**
 
70
     * Constructor: OpenLayers.Control.MousePosition
 
71
     *
 
72
     * Parameters:
 
73
     * options - {Object} Options for control.
 
74
     */
 
75
    initialize: function(options) {
 
76
        OpenLayers.Control.prototype.initialize.apply(this, arguments);
 
77
    },
 
78
 
 
79
    /**
 
80
     * Method: destroy
 
81
     */
 
82
     destroy: function() {
 
83
         if (this.map) {
 
84
             this.map.events.unregister('mousemove', this, this.redraw);
 
85
         }
 
86
         OpenLayers.Control.prototype.destroy.apply(this, arguments);
 
87
     },
 
88
 
 
89
    /**
 
90
     * Method: draw
 
91
     * {DOMElement}
 
92
     */
 
93
    draw: function() {
 
94
        OpenLayers.Control.prototype.draw.apply(this, arguments);
 
95
 
 
96
        if (!this.element) {
 
97
            this.div.left = "";
 
98
            this.div.top = "";
 
99
            this.element = this.div;
 
100
        }
 
101
 
 
102
        this.redraw();
 
103
        return this.div;
 
104
    },
 
105
 
 
106
    /**
 
107
     * Method: redraw
 
108
     */
 
109
    redraw: function(evt) {
 
110
 
 
111
        var lonLat;
 
112
 
 
113
        if (evt == null) {
 
114
            lonLat = new OpenLayers.LonLat(0, 0);
 
115
        } else {
 
116
            if (this.lastXy == null ||
 
117
                Math.abs(evt.xy.x - this.lastXy.x) > this.granularity ||
 
118
                Math.abs(evt.xy.y - this.lastXy.y) > this.granularity)
 
119
            {
 
120
                this.lastXy = evt.xy;
 
121
                return;
 
122
            }
 
123
 
 
124
            lonLat = this.map.getLonLatFromPixel(evt.xy);
 
125
            if (!lonLat) {
 
126
                // map has not yet been properly initialized
 
127
                return;
 
128
            }
 
129
            if (this.displayProjection) {
 
130
                lonLat.transform(this.map.getProjectionObject(),
 
131
                                 this.displayProjection );
 
132
            }
 
133
            this.lastXy = evt.xy;
 
134
 
 
135
        }
 
136
 
 
137
        var newHtml = this.formatOutput(lonLat);
 
138
 
 
139
        if (newHtml != this.element.innerHTML) {
 
140
            this.element.innerHTML = newHtml;
 
141
        }
 
142
    },
 
143
 
 
144
    /**
 
145
     * Method: formatOutput
 
146
     * Override to provide custom display output
 
147
     *
 
148
     * Parameters:
 
149
     * lonLat - {<OpenLayers.LonLat>} Location to display
 
150
     */
 
151
    formatOutput: function(lonLat) {
 
152
                var inches=OpenLayers.INCHES_PER_UNIT;
 
153
                var metersPerPixel = this.map.getResolution()*(inches[this.map.getUnits()]*(1/inches['m']));
 
154
                var mgdigits = parseInt(6-Math.ceil(Math.log(metersPerPixel)/2.302585092994046));
 
155
        var digits = parseInt(this.numDigits);
 
156
        var mgrs = new USNG2();
 
157
        if ((lonLat.lat > -80) && (lonLat.lat < 80)) {
 
158
            var mgrsStr = mgrs.fromLonLat(lonLat, mgdigits-1);
 
159
        } else {
 
160
            var mgrsStr = 'undefined';
 
161
        }
 
162
        var newHtml =
 
163
            this.prefix +
 
164
            lonLat.lon.toFixed(digits) +
 
165
            this.separator +
 
166
            lonLat.lat.toFixed(digits) +
 
167
            " / MGRS: " + mgrsStr +
 
168
            this.suffix;
 
169
        return newHtml;
 
170
     },
 
171
 
 
172
    /**
 
173
     * Method: setMap
 
174
     */
 
175
    setMap: function() {
 
176
        OpenLayers.Control.prototype.setMap.apply(this, arguments);
 
177
        this.map.events.register( 'mousemove', this, this.redraw);
 
178
    },
 
179
 
 
180
    CLASS_NAME: "OpenLayers.Control.MousePosition"
 
181
});