2
* @author Patrick Schmiedel patrick.schmiedel@gmx.net
6
* @classDescription A user interface element that shows the latitude/longitude value of the location under the mouse cursor.
8
/*global Class, $, $H, Event, Element */
9
var SunCoordsDisplay = Class.create();
11
SunCoordsDisplay.prototype = Object.extend({
18
* @param {Sring} elementId The ID of the SunCoordsDisplay HTML element.
19
* @param {TileLayerProvider} layerProvider The corresponding TileLayerProvider.
20
* @param {Hash} options Available options: decimals
22
initialize: function (elementId, layerProvider, options) {
23
this.domNode = $(elementId);
24
this.layerProvider = layerProvider;
26
Object.extend(this, this.defaultOptions.toObject());
27
Object.extend(this, options);
29
this.createElements();
31
Event.observe(this.layerProvider.tileContainer.viewport.domNode, 'mousemove', this.updateCoords.bindAsEventListener(this));
35
* @method createElements Creates the user interface HTML elements.
37
createElements: function () {
38
var table = new Element('table', {'class': 'sunCoordsDisplay'});
39
var tr = new Element('tr');
40
this.latText = new Element('td', {style: 'width: 15px'});
41
this.latValue = new Element('td', {style: 'width: 25px'});
42
this.lonText = new Element('td', {style: 'width: 15px'});
43
this.lonValue = new Element('td', {style: 'width: 25px'});
44
tr.appendChild(this.latText);
45
tr.appendChild(this.latValue);
46
tr.appendChild(this.lonText);
47
tr.appendChild(this.lonValue);
48
table.appendChild(tr);
49
this.domNode.appendChild(table);
53
* TODO: Move to LatLon.fromXY()
54
* @method XYToRad Converts (x,y) pixel coordinates in the viewport to latitude/longitude sun coordinates.
55
* @param {Number} x The x coordinate in the viewport in pixels.
56
* @param {Number} y The y coordinate in the viewport in pixels.
57
* @return {Hash} The latitude/longitude coordinates or null if out of bounds.
59
XYToRad: function (x, y) {
60
if (!this.layerProvider.sunImage) {
63
var sunRadius = this.layerProvider.sunImage.sunRadius * this.layerProvider.getFullSize();
64
var viewerPosition = this.layerProvider.tileContainer.viewport.position;
65
var y1 = (y - viewerPosition.y) / sunRadius;
66
if (-1 <= y1 && y1 <= 1) {
67
var latitudeRad = Math.asin(y1);
69
var x1 = (x - viewerPosition.x) / sunRadius;
70
var x2 = x1 / Math.cos(latitudeRad);
71
if (-1 <= x2 && x2 <= 1) {
72
var longitudeRad = Math.asin(x2);
74
return { latitude: latitudeRad, longitude: longitudeRad };
81
* @method displayCoords Shows the latitude/longitude values in the user interface element.
82
* @param {Number} latitude The latitude value.
83
* @param {Number} longitude The longitude value.
85
displayCoords: function (latitude, longitude) {
86
var showCoords = (latitude !== null && longitude !== null);
87
this.latValue.innerHTML = showCoords ? Math.abs(latitude.toFixed(this.decimals)) : '';
88
this.latText.innerHTML = showCoords ? (latitude < 0 ? 'N' : 'S') : '';
89
this.lonValue.innerHTML = showCoords ? Math.abs(longitude.toFixed(this.decimals)) : '';
90
this.lonText.innerHTML = showCoords ? (longitude < 0 ? 'E' : 'W') : '';
94
* @method updateCoords Calculates the sun coordinates and shows them in the user interface.
95
* @param {DOM MouseEvent} e The DOM MouseEvent.
97
updateCoords: function (e) {
98
if (this.layerProvider.tileContainer.viewport.surface.moving) {
101
var viewportOffset = this.layerProvider.tileContainer.viewport.offset;
103
x: Event.pointerX(e) - viewportOffset[0],
104
y: Event.pointerY(e) - viewportOffset[1]
106
var screenSunCoords = this.XYToRad(screenCoords.x, screenCoords.y);
107
if (screenSunCoords === null) {
108
this.displayCoords();
110
var realSunCoordsDeg = {
111
latitude: screenSunCoords.latitude + this.layerProvider.sunImage.date.getObliquity(),
112
longitude: screenSunCoords.longitude
114
this.displayCoords(realSunCoordsDeg.latitude.toDeg(), realSunCoordsDeg.longitude.toDeg());
b'\\ No newline at end of file'