发布时间:2025-12-10 13:01:23 浏览次数:1
HTML5地理定位导航代码可以让网站拥有更加智能的定位功能,以便更好的开发出适合用户的网站。
// 检测是否支持Geolocation APIif (navigator.geolocation) {// 支持,获取位置信息navigator.geolocation.getCurrentPosition(function (position) {// 获取纬度、经度var latitude = position.coords.latitude;var longitude = position.coords.longitude;// 在地图上标记出当前位置var map = new BMap.Map("allmap");// 创建地图中心点var centerPoint = new BMap.Point(longitude, latitude);// 设置缩放比例map.centerAndZoom(centerPoint, 17);var marker = new BMap.Marker(centerPoint);map.addOverlay(marker);}, function (error) {// 获取位置失败switch (error.code) {case error.TIMEOUT:alert("获取位置超时,请重试");break;case error.PERMISSION_DENIED:alert("您拒绝了位置共享申请");break;case error.POSITION_UNAVAILABLE:alert("位置信息不可用");break;}});} else {// 不支持alert("您的浏览器不支持当前拍摄功能");}上面的代码是通过Geolocation API获取当前位置,并使用百度地图API将位置在地图上标记,以便用户更加直观的了解自己的位置。