H5获取地理位置

来源:互联网 发布:长颈鹿美语待遇 知乎 编辑:程序博客网 时间:2024/05/27 00:49

H5获取地理位置

<!DOCTYPE html><html>    <head>        <meta charset="UTF-8">        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />        <title>H5获取地理位置</title>    </head>    <body>        <script type="text/javascript">            if(navigator.geolocation) {                navigator.geolocation.getCurrentPosition(getLocation, showError);            } else {                alert("浏览器不支持")            }            // 返回经度和纬度            function getLocation(position) {                lat = position.coords.latitude;                lng = position.coords.longitude;                alert(lat + "," + lng);            }            //处理错误和拒绝            function showError(error) {                switch(error.code) {                    case error.TIMEOUT:                        alert("请求超时!请再试一次!");                        break;                    case error.POSITION_UNAVAILABLE:                        alert('我们找不到你的位置 Sorry!  ios10必须https才能访问定位');                        break;                    case error.PERMISSION_DENIED:                        alert('请允许地理位置访问!');                        break;                    case error.UNKNOWN_ERROR:                        alert('发生了未知错误!');                        break;                }            }        </script>    </body></html>

这里写图片描述

原创粉丝点击