- <!DOCTYPE html>
- <html>
- <head>
- <title>demo1</title>
- <style>
- p{
- margin: 5px 0;
- padding:5px;
- border:1px solid #003333;
- background:#00cccc;
- height:50px;
- text-align:center;
- color:#000000;
- width:200px;
- }
- </style>
- <meta charset="utf-8" />
- </head>
- <body>
- <input type="button" id="go" value ="Click Me To View Your Location" />
- <p id="para"></p>
- <script>
- window.onload=function() {
- var par = document.getElementById("para");
- geo(par,2);
- }
- function geo(elem1, index){
- if(navigator&&navigator.geolocation){
- if(index==1){navigator.geolocation.watchPosition(suc_geo, err_geo)}
- else if(index==2){navigator.geolocation.getCurrentPosition(suc_geo, err_geo)}
- }
- function suc_geo(position){
- printGeo(position.coords.latitude, position.coords.longitude, position.coords.accuracy)
- }
- function err_geo(err){
- if(err.code==0){
- error("Geolocation is denied");
- }
- else if(err.code==1){
- error("Position Unavailable");
- }
- else if(err.code==2){
- error("Request is Timeout")
- }
- }
- function printGeo(lat, longi, acc){
- elem1.innerHTML = "Latitude: " + lat + ";" + "</br>" + "Longitude: " + longi + ";"
- +"</br>" + "Accuracy: " + acc + "";
- }
- }
- </script>
- </body>
- </html>
- //该片段来自于http://www.codesnippet.cn/detail/200820135240.html
来源: http://www.codesnippet.cn/detail/200820135240.html