File tree Expand file tree Collapse file tree 1 file changed +14
-16
lines changed Expand file tree Collapse file tree 1 file changed +14
-16
lines changed Original file line number Diff line number Diff line change 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 >
You can’t perform that action at this time.
0 commit comments