<script>
if(getCookie('latitude')==null || getCookie('latitude')==undefined){
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition,showError,{
// 指示浏览器获取高精度的位置,默认为false
enableHighAcuracy : true,
// 指定获取地理位置的超时时间,默认不限时,单位为毫秒
timeout : 5000,
// 最长有效期,在重复获取地理位置时,此参数指定多久再次获取位置。
maximumAge : 2000
});
}else{
alert("当前设备不支持定位");
}
}
function showPosition(position){
document.cookie="latitude="+position.coords.latitude;
document.cookie="longitude="+position.coords.longitude;
if(location.href.indexOf("#reloaded")==-1){
location.href=location.href+"#reloaded";
location.reload();
}
}
// function showPosition(position){
// alert(position);
// new BMap.Point(position.coords.longitude,position.coords.latitude);
// const geolocation = new BMap.Geolocation();
// geolocation.getCurrentPosition(function(r) {
// if (this.getStatus() == BMAP_STATUS_SUCCESS) {
// document.cookie="latitude="+r.point.lat;
// document.cookie="longitude="+r.point.lng;
// if(location.href.indexOf("#reloaded")==-1){
// location.href=location.href+"#reloaded";
// location.reload();
// }
// }else {
// alert('获取失败'+this.getStatus());
// }
// },{enableHighAccuracy: true});
// }
function showError(error){
switch(error.code) {
case error.PERMISSION_DENIED:
alert("定位失败,用户拒绝请求地理定位");
break;
case error.POSITION_UNAVAILABLE:
alert("定位失败,位置信息是不可用");
break;
case error.TIMEOUT:
alert("定位失败,请求获取用户位置超时");
break;
case error.UNKNOWN_ERROR:
alert("定位失败,定位系统失效");
break;
}
}
function getCookie($name){
const data = document.cookie;
const dataArray = data.split("; ");
for(let i=0; i<dataArray.length; i++){
const varName = dataArray[i].split("=");
if(varName[0]==$name){
return decodeURI(varName[1]);
}
}
}
//删除cookie中指定变量函数
function delCookie($name){
const myDate = new Date();
myDate.setTime(-1000);//设置时间
document.cookie=$name+"=''; expires="+myDate.toGMTString();
}
</script>