返回列表 发新帖
查看: 812|回复: 0

[HTML+CSS+JS特效] 使用 getCurrentPosition() 方法来获得用户的位置

[复制链接]

6671

热度

1万

元宝

262

贡献

管理员

DZ专员

发表于 2014-12-15 16:11:03 | 显示全部楼层 |阅读模式

基本在现在的手机浏览器上面测试都可以通过
  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <p id="demo">点击这个按钮,获得您的坐标:</p>
  5. <button>试一下</button>
  6. <script>
  7. var x=document.getElementById("demo");
  8. function getLocation()
  9.   {
  10.   if (navigator.geolocation)
  11.     {
  12.     navigator.geolocation.getCurrentPosition(showPosition);
  13.     }
  14.   else{x.innerHTML="Geolocation is not supported by this browser.";}
  15.   }
  16. function showPosition(position)
  17.   {
  18.   x.innerHTML="Latitude: " + position.coords.latitude +
  19.   "<br />Longitude: " + position.coords.longitude;        
  20.   }
  21. </script>
  22. </body>
  23. </html>
复制代码

返回列表 发新帖
 懒得打字嘛,点击右侧快捷回复【最新发布】   【赞助草根吧享更多权益】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

收藏帖子 返回列表 搜索

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

小黑屋|手机版|草根吧