Jas*_*rod 56 javascript html5 geolocation deprecation-warning
我在我的网站上收到此错误,该网站向用户请求地理位置数据:
getCurrentPosition()和watchPosition()在不安全的起源上被弃用,将来会删除支持.您应该考虑将应用程序切换到安全的来源,例如HTTPS.有关详细信息,请参阅goo.gl/rStTGz.
我的意思是它基本上只是一个通知,谷歌链接只是说它被弃用了.
我没有计划将我的网站转移到SSL ...那么像我这样的人有替代方案吗?
gog*_*son 42
因为根据您的体系结构切换到HTTPS可能很痛苦或不可能,我找到了一种解决方法:您可以使用Google Maps Geolocation API.虽然它有使用限制,但它可以完成这项工作.您将需要一个浏览器API密钥,因此不要忘记将其使用限制在您的页面主机名中.
getCurrentPosition()如果失败,我将它用作方法的后备方法.它允许我让它工作,直到我切换到HTTPS.
这是JSFiddles:
小智 16
在/ jstillwell的帖子中找到了一个可能的答案:https: //github.com/stefanocudini/leaflet-gps/issues/15 基本上这个功能将来不支持(仅限Chrome?),但仅适用于HTTP站点.HTTPS仍然可以,并且没有计划为HTTP使用创建等效的替代品.
Arc*_*dze 16
它仅用于测试,您可以在 google chrome 中进行:导航到:chrome://flags/#unsafely-treat-insecure-origin-as-secure
然后您会看到:
键入您要允许的地址,然后启用并重新启动浏览器。
Asi*_*K T 14
是.谷歌浏览器已弃用版本50中的功能.如果您尝试在Chrome中使用它,则错误为:
getCurrentPosition() and watchPosition() are deprecated on insecure origins. To use this feature, you should consider switching your application to a secure origin, such as HTTPS. See https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-powerful-features-on-insecure-origins for more details.
因此,您必须添加SSL证书.嗯,这是唯一的方法.
现在使用Let's Encrypt很容易.这是指南
出于测试目的,您可以尝试这样做:
1.localhost被视为HTTP上的安全源,因此如果您能够从localhost运行服务器,则应该能够在该服务器上测试该功能.
2.您可以使用--unsafely-treat-insecure-origin-as-secure ="http://example.com"标志运行chrome(将"example.com"替换为您实际要测试的原点),将该来源视为本次会议的安全.请注意,您还需要包含--user-data-dir =/test/only/profile/dir来创建新的测试配置文件以使该标志生效.
我认为Firefox还限制用户访问来自的GeoLocation API请求http.这是webkit更改日志:https://trac.webkit.org/changeset/200686
Ben*_*ing 13
您可以使用https://ipinfo.io API(这是我的服务).它可以免费获得高达1,000 req/day(有或没有SSL支持).它为您提供坐标,名称等.这是一个例子:
curl ipinfo.io
{
"ip": "172.56.39.47",
"hostname": "No Hostname",
"city": "Oakland",
"region": "California",
"country": "US",
"loc": "37.7350,-122.2088",
"org": "AS21928 T-Mobile USA, Inc.",
"postal": "94621"
}
Run Code Online (Sandbox Code Playgroud)
这是一个用API响应构造coords对象的示例,它与您的结果相匹配getCurrentPosition():
$.getJSON('https://ipinfo.io/geo', function(response) {
var loc = response.loc.split(',');
var coords = {
latitude: loc[0],
longitude: loc[1]
};
});
Run Code Online (Sandbox Code Playgroud)
这里有一个详细的例子,展示了如何将它用作后备getCurrentPosition():
function do_something(coords) {
// Do something with the coords here
}
navigator.geolocation.getCurrentPosition(function(position) {
do_something(position.coords);
},
function(failure) {
$.getJSON('https://ipinfo.io/geo', function(response) {
var loc = response.loc.split(',');
var coords = {
latitude: loc[0],
longitude: loc[1]
};
do_something(coords);
});
};
});
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅http://ipinfo.io/developers/replacing-navigator-geolocation-getcurrentposition.
小智 6
您可以使用--unsafely-treat-insecure-origin-as-secure =“ http://example.com”标志(将“ example.com”替换为您实际要测试的来源)来运行chrome,该来源在此会话中是安全的。请注意,您还需要包括--user-data-dir = / test / only / profile / dir来创建新的测试配置文件,以使标志起作用。
例如,如果使用Windows,则单击“开始并运行”。
chrome --unsafely-treat-insecure-origin-as-secure="http://localhost:8100" --user-data-dir=C:\testprofile
Run Code Online (Sandbox Code Playgroud)