H5获取地理位置

xiaoxiao2021-02-28  62

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>

转载请注明原文地址: https://www.6miu.com/read-84709.html

最新回复(0)