[英]W3C Geolocation API not working in Chrome
以下代碼適用於Firefox,但不適用於Google Chrome:
<!DOCTYPE html>
<html>
<head>
<title>title</title>
<script type="text/javascript">
var successCallback = function(data) {
console.log('latitude: ' + data.coords.latitude + ' longitude: ' + data.coords.longitude);
};
var failureCallback = function() {
console.log('location failure :(');
};
var logLocation = function() {
//determine if the handset has client side geo location capabilities
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(successCallback, failureCallback);
}
else{
alert("Functionality not available");
}
};
logLocation();
setTimeout(logLocation, 5000);
</script>
</head>
<body>
<p>Testing</p>
<body>
</html>
這是怎么回事? 我認為谷歌Chrome應該支持W3C Geolocation API。
對我來說非常適合 - 在Win 7上使用Chrome 11和Firefox 4.0.1
Options > Under the Hood > Content Settings > Location
file:///
scheme加載的資源訪問位置。 請參閱Chrome中的HTML 5地理位置提示 。 如果您的域不安全(例如HTTP而不是HTTPS),則不允許您訪問Chrome中的位置。 這是自Chrome版本50(太平洋標准時間2016年4月20日下午12點)。
有關詳細信息,請參閱https://developers.google.com/web/updates/2016/04/geolocation-on-secure-contexts-only 。
在2017年:
注意:從Chrome 50開始,Geolocation API僅適用於HTTPS等安全上下文。 如果您的站點托管在非安全源(例如HTTP)上,則獲取用戶位置的請求將不再起作用。
它適用於我 - 在Win 7上同時使用Chrome 11和Firefox 4.0.1
確保您未在Chrome中停用位置跟蹤:選項>在引擎蓋>內容設置>位置,請允許許可,並在檢查權限后運行它
在運行之后它將是sucesscallback
或者它來到errorcallback
function sucesscallback (position) { var userLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); var myOptions = { zoom: 15, center: userLatLng, mapTypeId: google.maps.MapTypeId.ROADMAP }; var mapObject = new google.maps.Map(document.getElementById("googleMap"), myOptions); var marker = new google.maps.Marker({ map: mapObject, position: userLatLng }); } function failureCallback(error) { switch (error.code) { case 1: alert("User denied the request for Geolocation."); break; case 2: alert("Location information is unavailable. Please ensure Location is On"); break; case 3: alert("timeout"); break; case 4: alert("An unknown error occurred."); break; } }
<div id='googleMap' style='width:300px;height:300px;'> </div>
通過Geolocation API,您可以在用戶同意的情況下發現用戶的位置。 您可以將此功能用於引導用戶到目的地以及對用戶創建的內容進行地理標記等操作; 例如,標記拍攝照片的位置。
Geolocation API還允許您查看用戶所在的位置,並在用戶移動時始終關注它們,始終在用戶同意的情況下(並且僅在頁面打開時)。 這會創建許多有趣的用例,例如,如果用戶在附近,則與后端系統集成以准備收集訂單。
使用Geolocation API時需要注意很多事情。 本指南將指導您完成常見的用例和解決方案。
https://developers.google.com/web/fundamentals/native-hardware/user-location/?hl=en
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.