1 回答

TA貢獻1796條經驗 獲得超4個贊
好的,運行一些測試后的一些事情:
上面的“按鈕”沒有任何問題,Mac 和 iOS 上的 Safari 都可以正常響應
onclick="function()"
。geolocation
如果頁面不是通過.Safari 訪問,Safari 將拒絕所有訪問https:
。嘗試訪問geolocation
withhttp:
將引發在控制臺中可見的錯誤,否則該錯誤將保持沉默。如果 Safari 試圖通過 訪問
geolocation
,https
瀏覽器會彈出一個對話框,上面寫著“網站 [your_site_name] 想使用您當前的位置?!?,帶有“不允許”和“允許”按鈕。
如果用戶設法克服所有這些障礙,geolocation就會奏效。
測試代碼:
<html>
<head>
</head>
<body>
<form id="locinput">
<input id="clickMe" type="button" value="Get GeoLoc"
onclick="getLocation();"/>
<button value="iosbutton" type="button" onclick="getLocation();">iOS
Button
</button>
<a id="iosButton" class="btn" data-g-label="button1"
href="#" onclick="getLocation();">Get Location</a>
<input type="button" value="Test" onclick="getLocation();"/>
</form>
<script>
const form = document.getElementById('locinput');
function getLocation () {
console.log('getLocation()');
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(prefillLocation);
} else {
alert('Geolocation is not supported by this browser.');
}
}
function prefillLocation (position) {
form.value = position.coords.latitude + ', ' + position.coords.longitude;
}
</script>
</body>
</html>
添加回答
舉報