javascript 未经许可的地理定位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15017854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
geolocation without requesting permission
提问by Brady Moritz
Ive noticed that modern html5 based geolocation always asks the user "do you want to share your location with this website?". Which is fine, but I know there are other routes of trying to determine a ballpark geolocation without having to request this permission. If I recall, these services uses ip databases to try to track geolocaiton info and provide it to a web app.
我注意到现代基于 html5 的地理定位总是询问用户“你想与这个网站分享你的位置吗?”。这很好,但我知道还有其他方法可以尝试确定棒球场的地理位置,而无需请求此许可。如果我记得,这些服务使用 ip 数据库来尝试跟踪地理定位信息并将其提供给网络应用程序。
So, in my website I would like to get a "best guess" at the user's zip code, without the geolocation permission being requested. What's the simplest and/or best method of doing this?
因此,在我的网站中,我想对用户的邮政编码进行“最佳猜测”,而无需请求地理定位许可。这样做的最简单和/或最好的方法是什么?
回答by Ben Dowling
IP geolocation is less accurate, but you don't need user permission for it. The http://ipinfo.ioAPI (which is my service) makes it extremely simple. Here's a jQuery example:
IP 地理定位不太准确,但您不需要用户许可。该http://ipinfo.ioAPI(这是我的服务),使得它非常简单。这是一个 jQuery 示例:
$.get("http://ipinfo.io", function(response) {
console.log(response.city, response.region, response.country);
}, "jsonp");
More details are available here.
可在此处获得更多详细信息。
回答by Philip
Use a IP location script on the back-end using the _SERVER
variables. This is the only way without permission, but with huge disadvantages:
1) It is not accurate at all
2) The IP address can be altered by the user if they're using a proxy
使用_SERVER
变量在后端使用 IP 位置脚本。这是未经许可的唯一方法,但有很大的缺点:1) 它根本不准确 2) 如果用户使用代理,IP 地址可以被用户更改
But still, it's the only way if you don't want to ask permission.
但是,如果您不想征得许可,这是唯一的方法。
Or you could just ask the user for their zip code, since they want to use you site, they might as well :)
或者您可以询问用户他们的邮政编码,因为他们想使用您的网站,他们也可以:)
Also, have a look at this post: Geolocation without Prompt
另外,看看这篇文章:没有提示的地理定位
回答by Jonathan
With the https://ipdata.coAPI
使用https://ipdata.coAPI
$.get("https://api.ipdata.co", function (response) {
$("#response").html(JSON.stringify(response, null, 4));
}, "jsonp");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre id="response"></pre>