-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
97 lines (81 loc) · 2.41 KB
/
script.js
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
var isGeohash2Coordinate = true;
var precision = 1;
$('#txtPrecision').val(precision);
$('#btnConvert').click(function () {
var inputs = $('#txtInput').val().split('\n');
var outputs = '';
for (input of inputs) {
if (isGeohash2Coordinate)
{
outputs += convertToCoordinate(input) + '\n';
}
else
{
outputs += convertToGeoHash(input) + '\n';
}
}
$('#txtOutput').val(outputs);
});
$('#btnChangeDirection').click(function() {
isGeohash2Coordinate = !isGeohash2Coordinate;
$('#frmPrecision').toggle();
if (isGeohash2Coordinate)
{
$('#txtInput').attr('placeholder', 'one geohash input per line');
$('#lblInput').html("Geohash(es):");
$('#lblOutput').html("Coordinate(s):");
}
else {
$('#txtInput').attr('placeholder', 'one coordinate input per line: latitude, longitude, precision(optional)');
$('#lblInput').html("Coordinate(s):");
$('#lblOutput').html("Geohash(es):");
}
});
$('#btnClear').click(function() {
$('#txtInput').val('');
$('#txtOutput').val('');
});
function convertToCoordinate(input) {
var coordinate = decodeGeoHash(input);
if (!coordinate || !coordinate.latitude[2] || !coordinate.longitude[2]) {
return '-=error=-';
}
return `${coordinate.latitude[2].toFixed(8)},${coordinate.longitude[2].toFixed(8)},${input.length}`;
}
function convertToGeoHash(input) {
var inputArray = input.split(',');
if (inputArray.length >= 2) {
var lat = inputArray[0].trim();
var lng = inputArray[1].trim();
var coordinatePrecision = precision;
if (inputArray.length > 2) {
var coordinatePrecision = inputArray[2].trim();
}
if (/^(\-?\d+(\.\d+)?)$/.test(lat) && /^(\-?\d+(\.\d+)?)$/.test(lng)) {
return encodeGeoHash(lat, lng, coordinatePrecision);
}
}
return '-=error=-';
}
function validatePrecision() {
var value = $('#txtPrecision').val();
if (!value) {
return '';
}
else if (!$.isNumeric(value)) {
return 1;
}
else if (value > 12) {
return 12;
}
else if (value < 1) {
return 1;
}
return value;
}
$('#txtPrecision').change(function () {
$('#txtPrecision').val(validatePrecision());
});
$('#txtPrecision').keyup(function () {
$('#txtPrecision').val(validatePrecision());
});