使用 google.loader.ClientLocation 获取用户位置对我有用而不是其他人 [英] Getting user location with google.loader.ClientLocation working for me but not others

查看:25
本文介绍了使用 google.loader.ClientLocation 获取用户位置对我有用而不是其他人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 google.loader.ClientLocation 和地图 API 来获取访问者的位置并将地图置于其中心.以下对我来说工作正常(在 Safari、Firefox 和 Chrome 上),但我测试它的朋友(在 Safari 和 Firefox 中)只看到一个带有 Google 徽标的白框.

Am trying to use google.loader.ClientLocation along with the maps api to get the location of the visitor and center a map on it. The following is working fine for me (on Safari, Firefox and Chrome), but a friend I got to test it (in Safari and Firefox) just sees a white box with the Google logo.

 <script type="text/javascript" src="http://www.google.com/jsapi?key=MykeyHere"></script>

  <script type="text/javascript">
   google.load("maps", "2");
  </script>

  <script type="text/javascript"> 

      function initialize() {
        if (GBrowserIsCompatible()) {
          var map = new GMap2(document.getElementById("local_map"));
    map.setMapType(G_PHYSICAL_MAP);
    map.setCenter(new GLatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude), 15);
          // map.setCenter(new GLatLng(54.5975, -5.920278), 15);
          map.setUIToDefault();

        } 

      </script>

任何想法可能会出错?该网站位于 http://www.chris-armstrong.com/ticktalk(地图靠近底部)

Any ideas what could be going wrong? The site is at http://www.chris-armstrong.com/ticktalk (the map is near the bottom)

推荐答案

Google ClientLocation 使用 IP 地址地理定位.有几个 IP 地址地理定位提供商(右侧边栏中的相关问题将为您提供其中几个),它们为不同的用户提供不同的结果.Google ClientLocation 无法定位到所有人,也不会总是像某些竞争对手一样准确或准确地给出答案.

Google ClientLocation uses IP address geolocation. There are several IP address geolocation providers out there (the related questions in the sidebar on the right will give you several of those) and they give different results to different users. Google ClientLocation won't be able to locate everyone and it won't always give answers that are as precise or as accurate as some competitors.

例如,在 我的测试页上,我使用 Google ClientLocation 和 IPInfoDB 和 Google ClientLocation 现在找不到我在家,而 IPInfoDB 认为我在桑尼维尔而不是伯克利.您可能会发现一个提供者比其他提供者更好(特别是如果您为其数据库付费时),但我怀疑最可靠的方法,如果您真的需要它,是使用多个并希望至少一个会有结果.您应该事先知道 IP 地理定位并不总是适用于所有用户(例如,匿名代理背后的用户永远不会以这种方式定位).

On my test page, for example, I use Google ClientLocation and IPInfoDB and Google ClientLocation doesn't find me at home right now, while IPInfoDB thinks I'm in Sunnyvale instead of Berkeley. You may find that one provider is better than the others (particularly if you pay for its database), but I suspect that the most robust method, if you really need it, is to use several and hope that at least one will have a result. You should know up front that IP geolocation will not always work for all users (those behind an anonymizing proxy, for example, will never be located this way).

另外,如果您想要更精确的位置,您可以使用 Javascript API 向浏览器询问用户的确切位置.在受支持的浏览器上,用户将收到一个请求他们许可的小弹出窗口,如果允许,浏览器将使用 GPS(例如 iPhone 上的 Mobile Safari)或 WiFi 三角测量(Firefox 3.5+)来确定纬度和经度.您可以在我的测试页面上查看其工作原理,并将其与 IP 地理位置进行比较.并非所有浏览器都支持 W3C Geolocation API,但越来越多的浏览器支持.

Also, if you want a more precise location, you can use a Javascript API to ask the browser for the user's exact location. On supported browsers the user will receive a small pop-up asking for their permission and, if given, the browser will then use GPS (Mobile Safari on the iPhone, for example) or WiFi triangulation (Firefox 3.5+) to determine latitude and longitude. You can see how this works on my test page, and compare it with IP geolocation. Not all browsers support the W3C Geolocation API, but a growing number do.

这篇关于使用 google.loader.ClientLocation 获取用户位置对我有用而不是其他人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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