您好,欢迎访问一九零五行业门户网

如何使用JS获取用户所在城市及地理位置

这次给大家带来如何使用js获取用户所在城市及地理位置,如何使用js获取用户所在城市及地理位置的注意事项有哪些,下面就是实战案例,一起来看一下。
<!doctype html> <html lang="en">  <head>  <meta charset="utf-8">  <title>获取用户地理位置</title>  <script type="text/javascript" src="./jquery-3.3.1.js"></script>  </head>  <body>  </body> </html> <script> $.getscript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js',function(){   alert(remote_ip_info.country);//国家   alert(remote_ip_info.province);//省份   alert(remote_ip_info.city);//城市  });  </script>
js获取用户地理位置
<script type="text/javascript">  var x = document.getelementbyid(x);  function getlocation() {    if (navigator.geolocation) {      navigator.geolocation.getcurrentposition(showposition, showerror);    } else {      x.innerhtml = 该浏览器不支持定位功能!;    }  }  function showposition(position) {    x.innerhtml = 纬度: + position.coords.latitude + \n经度:        + position.coords.longitude;  }  function showerror(error) {    switch (error.code) {    case error.permission_denied:      x.innerhtml = 用户拒绝对获取地理位置的请求。;      break;    case error.position_unavailable:      x.innerhtml = 位置信息是不可用的。;      break;    case error.timeout:      x.innerhtml = 请求用户地理位置超时。;      break;    case error.unknown_error:      x.innerhtml = 未知错误。;      break;    }  }  getlocation();</script>
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
express + mock如何操作前后台并行开发
filter自定义过滤器去重(附代码)
以上就是如何使用js获取用户所在城市及地理位置的详细内容。
其它类似信息

推荐信息