为什么我的任何浏览器都不支持地理定位?
Why geolocation is not supporting in my any browser?
为什么我的任何浏览器都不支持地理定位?
我是 javascript.
世界的新人
这是我在节点 js 服务器上 运行 的代码。
<!DOCTYPE html>
<html>
<head>
<title>Geolocation</title>
</head>
<body>
<button onclick="getLocation()">Get coords</button>
<h1 id="coords"></h1>
<script>
var x=document.getElementById('coords');
function getLocation(){
if (navigator.Geolocation) {
navigator.Geolocation.getCurrentPosition(showPosition);
}
else{
x.innerHTML="Geolocation is not supported";
}
function showPosition(position){
console.log(position.coords.latitude);
}
}
</script>
</body>
</html>
JavaScript 在变量名、API 等方面区分大小写。使用不带大写 G 的地理定位。
示例来自 MDN article on geolocation:
if ("geolocation" in navigator) {
/* geolocation is available */
} else {
/* geolocation IS NOT available */
}
你要注意大小写:把navigator.Geolocation)
改成navigator.geolocation
。
MDN 上的地理位置文档 API:https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/Using_geolocation
为什么我的任何浏览器都不支持地理定位? 我是 javascript.
世界的新人这是我在节点 js 服务器上 运行 的代码。
<!DOCTYPE html>
<html>
<head>
<title>Geolocation</title>
</head>
<body>
<button onclick="getLocation()">Get coords</button>
<h1 id="coords"></h1>
<script>
var x=document.getElementById('coords');
function getLocation(){
if (navigator.Geolocation) {
navigator.Geolocation.getCurrentPosition(showPosition);
}
else{
x.innerHTML="Geolocation is not supported";
}
function showPosition(position){
console.log(position.coords.latitude);
}
}
</script>
</body>
</html>
JavaScript 在变量名、API 等方面区分大小写。使用不带大写 G 的地理定位。
示例来自 MDN article on geolocation:
if ("geolocation" in navigator) {
/* geolocation is available */
} else {
/* geolocation IS NOT available */
}
你要注意大小写:把navigator.Geolocation)
改成navigator.geolocation
。
MDN 上的地理位置文档 API:https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/Using_geolocation