正在加载...

HTML geolocation

Geolocation 经用户授权由 JS 读取位置。

直接回答

navigator.geolocation.getCurrentPosition。无 HTML 标签。

Ask for position (requires permission)

html
HTML Code
<button id="loc" type="button">Share location</button>
<pre id="out"></pre>
<script>
document.getElementById("loc").onclick = function () {
if (!navigator.geolocation) {
document.getElementById("out").textContent = "No geolocation support";
return;
}
navigator.geolocation.getCurrentPosition(function (pos) {
document.getElementById("out").textContent =
pos.coords.latitude + ", " + pos.coords.longitude;
}, function () {
document.getElementById("out").textContent = "Permission denied or unavailable";
});
};
</script>

试一试

  1. http 提供页面
  2. 按钮授权/拒绝
  3. 两种 UI
  4. 明确用途

常见错误

  • .onload 就要位置
  • 忽略拒绝

小结

  • 需授权
  • HTML UI + JS
  • 优雅拒绝
  • 隐私优先

🧠 测试您的知识

准备开始

测试您的知识

通过这个互动测验挑战自己,看看你对这个主题的理解程度如何

❓
3
问题
🎯
70%
及格要求
♾️
∞
时间
🔄
∞
尝试次数

📝 说明

  • 仔细阅读每个问题
  • 为每个问题选择最佳答案
  • 您可以随时重新参加测验
  • 您的进度将显示在顶部