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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| <!DOCTYPE html>
<html lang="fr" dir="ltr">
<head>
<meta http-equiv="cache-control" content="public, max-age=60">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="Daniel Hagnoul">
<title>Test</title>
<style>
</style>
<script>
'use strict';
let GeoPoint = function(lon, lat) {
switch (typeof(lon)) {
case 'number':
this.lonDeg = this.dec2deg(lon, this.MAX_LON);
this.lonDec = lon;
break;
case 'string':
if (this.decode(lon)) {
this.lonDeg = lon;
}
this.lonDec = this.deg2dec(lon, this.MAX_LON);
break;
}
switch (typeof(lat)) {
case 'number':
this.latDeg = this.dec2deg(lat, this.MAX_LAT);
this.latDec = lat;
break;
case 'string':
if (this.decode(lat)) {
this.latDeg = lat;
}
this.latDec = this.deg2dec(lat, this.MAX_LAT);
break;
}
};
GeoPoint.prototype = {
CHAR_DEG : "\u00B0",
CHAR_MIN : "\u0027",
CHAR_SEC : "\u0022",
CHAR_SEP : "\u0020",
MAX_LON: 180,
MAX_LAT: 90,
// decimal
lonDec: NaN,
latDec: NaN,
// degrees
lonDeg: NaN,
latDeg: NaN,
dec2deg: function(value, max) {
var sign = value < 0 ? -1 : 1;
var abs = Math.abs(Math.round(value * 1000000));
if (abs > (max * 1000000)) {
return NaN;
}
var dec = abs % 1000000 / 1000000;
var deg = Math.floor(abs / 1000000) * sign;
var min = Math.floor(dec * 60);
var sec = (dec - min / 60) * 3600;
var result = "";
result += deg;
result += this.CHAR_DEG;
result += this.CHAR_SEP;
result += min;
result += this.CHAR_MIN;
result += this.CHAR_SEP;
result += sec.toFixed(2);
result += this.CHAR_SEC;
return result;
},
deg2dec: function(value) {
var matches = this.decode(value);
if (!matches) {
return NaN;
}
var deg = parseFloat(matches[1]);
var min = parseFloat(matches[2]);
var sec = parseFloat(matches[3]);
if (isNaN(deg) || isNaN(min) || isNaN(sec)) {
return NaN;
}
return deg + (min / 60.0) + (sec / 3600);
},
decode: function(value) {
var pattern = "";
// deg
pattern += "(-?\\d+)";
pattern += this.CHAR_DEG;
pattern += "\\s*";
// min
pattern += "(\\d+)";
pattern += this.CHAR_MIN;
pattern += "\\s*";
// sec
pattern += "(\\d+(?:\\.\\d+)?)";
pattern += this.CHAR_SEC;
return value.match(new RegExp(pattern));
},
getLonDec: function() {
return this.lonDec;
},
getLatDec: function() {
return this.latDec;
},
getLonDeg: function() {
return this.lonDeg;
},
getLatDeg: function() {
return this.latDeg;
}
};
document.addEventListener( 'DOMContentLoaded', ev => {
}, false );
window.addEventListener( 'load', ev => {
{
var lon = 24.0;
var lat = 15.876868;
var point = new GeoPoint(lon, lat);
console.log(point.getLonDeg()); // 24° 0' 0.00"
console.log(point.getLatDeg()); // 15° 52' 36.72"
}
{
var lon = 24.0;
var lat = -15.876868;
var point = new GeoPoint(lon, lat);
console.log(point.getLonDeg()); // 24° 0' 0.00"
console.log(point.getLatDeg()); // -15° 52' 36.72"
}
}, false );
</script>
</head>
<body>
<main>
</main>
</body>
</html> |
Partager