亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

地理位置不斷請求許可

地理位置不斷請求許可

ibeautiful 2022-09-16 21:29:37
我正在測試地理位置API,發現如果我刷新我的頁面,頁面會不斷請求權限,所以我將坐標數據保存到本地存儲,但它不起作用!有沒有辦法只授予一次許可???const COORDINATION = "coords";function saveCords(coordsOBJ){    localStorage.setItem(COORDINATION,JSON.stringify(coordsOBJ));}function handleGeoError(position){    console.log("Cant find position");}function handleGeoSuccess(position){   // console.log(position);   const latitude = position.coords.latitude;   console.log(latitude);   const longitude = position.coords.longitude;   const coordsOBJ = {       latitude,//latitude = latitude,       longitude//longitude = longitude   }   saveCords(coordsOBJ);}function askForCoords(){    navigator.geolocation.getCurrentPosition(handleGeoSuccess,handleGeoError);}function loadCoordinate(){    const loadedCords = localStorage.getItem("COORDINATION");    if(loadedCords === null)    {         askForCoords();    }}function init(){    loadCoordinate();}
查看完整描述

1 回答

?
一只甜甜圈

TA貢獻1836條經驗 獲得超5個贊

看起來你的代碼中有一個拼寫錯誤,你已經向協調添加了引號,但它是可變的而不是字符串。

嘗試更改:

const loadedCords = localStorage.getItem("COORDINATION");

自:

const loadedCords = localStorage.getItem(COORDINATION);


查看完整回答
反對 回復 2022-09-16
  • 1 回答
  • 0 關注
  • 81 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號