简体   繁体   English

没有请求许可的地理位置

[英]geolocation without requesting permission

Ive noticed that modern html5 based geolocation always asks the user "do you want to share your location with this website?". 我注意到现代的基于html5的地理定位总是要求用户“你想与这个网站分享你的位置吗?”。 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. 如果我记得,这些服务使用ip数据库来尝试跟踪geolocaiton信息并将其提供给Web应用程序。

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? 这样做最简单和/或最好的方法是什么?

IP geolocation is less accurate, but you don't need user permission for it. IP地理定位不太准确,但您不需要用户权限。 The http://ipinfo.io API (which is my service) makes it extremely simple. http://ipinfo.io API(这是我的服务)使它非常简单。 Here's a jQuery example: 这是一个jQuery示例:

$.get("http://ipinfo.io", function(response) {
    console.log(response.city, response.region, response.country);
}, "jsonp");

More details are available here . 更多细节可在此处获得

Use a IP location script on the back-end using the _SERVER variables. 使用_SERVER变量在后端使用IP位置脚本。 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 这是未经许可的唯一方法,但有很大的缺点: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 另外,看看这篇文章: 没有提示的地理位置

With the https://ipdata.co API 使用https://ipdata.co API

 $.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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM