geolocation without requesting permission

喜你入骨 提交于 2019-11-30 05:00:14

问题


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.

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?


回答1:


IP geolocation is less accurate, but you don't need user permission for it. The http://ipinfo.io API (which is my service) makes it extremely simple. Here's a jQuery example:

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

More details are available here.




回答2:


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

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




回答3:


With the 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>


来源:https://stackoverflow.com/questions/15017854/geolocation-without-requesting-permission

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!