-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
162 lines (142 loc) · 4.86 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>校园地图</title>
<style>
#map-container {
position: relative;
width: 100%;
max-width: 1000px;
margin: 0 auto;
}
#map {
width: 100%;
height: 0;
padding-top: 53.58%; /* (1378 / 2572) * 100 to maintain aspect ratio */
background-image: url('Map.png');
background-size: contain;
background-repeat: no-repeat;
background-position: center;
}
.marker {
position: absolute;
width: 10px;
height: 10px;
background-color: red;
border-radius: 50%;
transform: translate(-50%, -50%);
}
#message, #coordinates {
text-align: center;
margin-top: 10px;
}
#message {
color: red;
font-weight: bold;
}
#coordinates {
font-family: monospace;
}
i.fab {
font-size: 24px;
color: #000;
}
</style>
</head>
<body>
<h1>XJTLU地图</h1>
<h2>刷新以当前位置显示</h2>
<a href="https://github.com/Catherina0/XJTLUmap" target="_blank">
<i class="fab fa-github"></i>
</a>
<div id="map-container">
<div id="map"></div>
<div class="marker" id="marker"></div>
</div>
<div id="coordinates"></div>
<div id="message"></div>
<script>
// Updated affine transformation coefficients
const a = 0.000005088;
const b = 0;
const c = 121.148772;
const d = 0;
const e = -0.000004938;
const f = 31.486141;
// Image dimensions
const imageWidth = 2572;
const imageHeight = 1378;
function lonLatToPixel(lon, lat) {
// Inverse of the affine transformation
const x = (lon - c) / a;
const y = (lat - f) / e;
return { x, y };
}
function isWithinBounds(x, y) {
return x >= 0 && x <= imageWidth && y >= 0 && y <= imageHeight;
}
function showPosition(position) {
const lon = position.coords.longitude;
const lat = position.coords.latitude;
const pixel = lonLatToPixel(lon, lat);
const marker = document.getElementById('marker');
const message = document.getElementById('message');
const coordinates = document.getElementById('coordinates');
// Display coordinates
coordinates.textContent = `经度: ${lon.toFixed(6)}, 纬度: ${lat.toFixed(6)}`;
// Convert pixel coordinates to percentages
const xPercent = (pixel.x / imageWidth) * 100;
const yPercent = (pixel.y / imageHeight) * 100;
if (isWithinBounds(pixel.x, pixel.y)) {
marker.style.left = xPercent + '%';
marker.style.top = yPercent + '%';
marker.style.display = 'block';
message.textContent = '';
} else {
marker.style.display = 'none';
message.textContent = '超出区域';
}
}
function showError(error) {
const message = document.getElementById('message');
const coordinates = document.getElementById('coordinates');
coordinates.textContent = '';
switch(error.code) {
case error.PERMISSION_DENIED:
message.textContent = "用户拒绝了请求地理定位。";
break;
case error.POSITION_UNAVAILABLE:
message.textContent = "位置信息不可用。";
break;
case error.TIMEOUT:
message.textContent = "请求超时。";
break;
case error.UNKNOWN_ERROR:
message.textContent = "发生未知错误。";
break;
}
}
// Test
/*
const testPosition = {
coords: {
longitude: 121.154744,
latitude: 31.482953
}
};
showPosition(testPosition);
*/
// Production
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
document.getElementById('message').textContent = "该浏览器不支持定位。";
document.getElementById('coordinates').textContent = '';
}
</script>
</body>
<p>Producted by Catherina Grace</p>
</html>