地理位置无提示 [英] Geolocation without Prompt

查看:118
本文介绍了地理位置无提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道HTML5做地理定位的方法,但要注意的是它必须提示用户许可。

  navigator.geolocation.getCurrentPosition 

当我想在家中做地理定位时,它变得特别恼人页面根据位置提供自定义内容:每次访问您的网站时都不会收到提示。



这个想法是使用外部服务来做地理定位基于IP地址或类似的东西来获得对用户位置的较不准确的估计,并且仅当该位置真的关闭时才给予用户提示。对于我可以使用的服务有什么好的建议吗?



我知道Google已经完成了这项工作:当前位置。它不显示任何提示,显示您当前的位置。有谁知道如何做到这一点?



MaxMind数据库是好的,但这需要维护一个巨大的IP数据库,仅仅是为了这个目的。 API或Google服务是首选。



我目前使用Google Loader返回的位置对象来估算位置,但它不如Google精确搜索上面的例子。

  google.loader.ClientLocation.latitude 
google.loader.ClientLocation.longitude


解决方案

如果您不介意使用第三方API,则可以使用我们的服务 https://ip-api.io 。它使用客户端的IP地址,不需要用户的任何许可。



Javascript示例:



<$ p $ getJSON(http://ip-api.io/json/,
函数(结果){
console.log(结果);
});

然后您可以从 result.latitude result.longitude https://ip-api.io 提供更多数据,如国家,城市,邮政编码,时区以及是否此IP地址是bot,spammer或TOR节点。


I know about the HTML5 way to do geolocation, but the caveat is that it has to prompt the user for permission.

navigator.geolocation.getCurrentPosition

It gets particularly annoying when I would like to do geolocation right at the home page to serve customized content based on location: no one likes to get a prompt every time he or she visits your site.

The idea is to use external services to do geolocation based on IP address or something similar to get a less accurate estimate of the user's location and give the user a prompt only when that location is really off. Are there any good suggestions as to the services I can use for that purpose?

I know Google has done this already: current location. It shows your current location without giving you any prompt. Does anyone know how to do this?

The MaxMind database is good, but that would require the maintenance of a huge IP database just for that purpose. An API or a Google service is preferred.

I currently make use of the location object returned from the Google Loader to estimate the location, but it is less accurate than the Google search example above.

google.loader.ClientLocation.latitude
google.loader.ClientLocation.longitude

解决方案

If you don't mind using 3rd party API, you can use our service https://ip-api.io . It's working with client's IP address and doesn't require any permission from user.

Javascript example:

$.getJSON("http://ip-api.io/json/",
    function(result) {
        console.log(result);
    });

Then you can get location from result.latitude and result.longitude. https://ip-api.io provides much more data like country, city, zip code, timezone and whether this IP address is bot, spammer or TOR node.

这篇关于地理位置无提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆