【发布时间】:2018-11-28 09:22:16
【问题描述】:
我已经实现了devise omniauth 设置,当我使用 Facebook、Twitter 或 google 登录时,html5 geolocation 返回一个 position unavailable error。但是当我以常规devise user 登录时,它工作得很好!使用社交媒体帐户登录时,如何允许访问我的 rails 应用程序上的html5 geolocation?
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(setGeoCookie,showError);
} else {
alert("Geolocation is not supported by this browser.");
}
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
window.location = window.location;
window.alert("Permission denied");
break;
case error.POSITION_UNAVAILABLE:
window.location = window.location;
window.alert("Location information is unavailable.");
break;
case error.TIMEOUT:
window.location = window.location;
window.alert("The request to get user location timed out.");
break;
case error.UNKNOWN_ERROR:
window.location = window.location;
window.alert("An unknown error occurred.");
break;
}
location.reload();
}
更新 1
我将代码更改为以下内容,但在浏览器控制台中出现以下错误:
Origin does not have permission to use Geolocation service
[blocked] Access to geolocation was blocked over secure connection with mixed content to https://localhost:3000.
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error, options);
} else {
alert("Geolocation is not supported by this browser.");
}
}
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
function success(pos) {
var crd = pos.coords;
console.log('Your current position is:');
console.log(`Latitude : ${crd.latitude}`);
console.log(`Longitude: ${crd.longitude}`);
console.log(`More or less ${crd.accuracy} meters.`);
}
function error(err) {
console.warn(`ERROR(${err.code}): ${err.message}`);
}
关于如何解除阻止的任何想法?
【问题讨论】:
-
你是在本地主机上运行它吗?如果不是,您的网站正在通过 http 从 localhost 加载一些资产,迫使浏览器确定该网站不安全,并拒绝访问地理位置。此外,这是一个 javascript 问题,而不是 RoR 问题。
-
感谢@Tim Lawrenz 的回复。我在
https://localhost:3000上运行它,如下所示:rails server puma -b 'ssl://0.0.0.0:3000?key=server.key&cert=server.crt'。我怎样才能弄清楚确切的问题是什么?
标签: javascript ruby-on-rails geolocation omniauth