geolocation without requesting permission

2020-03-03 06:19发布

问题:

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>