Skip to content

Commit 6bef087

Browse files
committed
up2
1 parent 45b89fd commit 6bef087

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

test/index.html

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,20 @@
99
</head>
1010

1111
<body>
12-
<div>
13-
<span id="tset1"></span>----
14-
<span id="tset2"></span>
15-
</div>
12+
<p id="demo">点击这个按钮,获得您的坐标:</p>
13+
<button onclick="getLocation()">试一下</button>
1614
</body>
1715
<script>
18-
try {
19-
navigator.geolocation.getCurrentPosition(function (position) {
20-
longitude = position.coords.longitude;
21-
latitude = position.coords.latitude;
22-
document.getElementById("tset1").innerHTML = longitude;
23-
document.getElementById("tset2").innerHTML = latitude;
24-
})
25-
} catch (error) {
26-
document.getElementById("tset1").innerHTML = error;
27-
}
28-
</script>
29-
16+
var x=document.getElementById("demo");
17+
function getLocation(){
18+
if (navigator.geolocation){
19+
navigator.geolocation.getCurrentPosition(showPosition);
20+
}else{
21+
x.innerHTML="Geolocation is not supported by this browser.";
22+
}
23+
}
24+
function showPosition(position){
25+
x.innerHTML="Latitude: " + position.coords.latitude + "<br />Longitude: " + position.coords.longitude;
26+
}
27+
</script>
3028
</html>

0 commit comments

Comments
 (0)