代码之家  ›  专栏  ›  技术社区  ›  CodingWithoutComments

如何自动查找用户的位置?

  •  11
  • CodingWithoutComments  · 技术社区  · 16 年前

    我目前正在一个节目列表网站工作。我将按位置显示以各种不同方式排序的用户的显示信息。

    知道 我可以在用户首次登录网站时询问他们的位置,但我注意到许多网站都内置了这种自动检测位置的功能(例如,请参见 Last.fm “活动:您所在地区的音乐会列表”)。

    他们是怎么做到的?我正在用ruby on rails构建我的网站。

    5 回复  |  直到 14 年前
        1
  •  9
  •   GEOCHET S.Lott    16 年前

    以下链接指向相关google maps api上的文档:

    http://code.google.com/apis/ajax/documentation/#ClientLocation

    它展示了如何使用它的示例:

    /**
     * Set the currentState_ and currentCountry_ properties based on the client's
     * current location (when available and in the US), or to the defaults.
     */
    InTheNews.prototype.setDefaultLocation_ = function() {
      this.currentState_ = this.options_.startingState;
      if (google.loader.ClientLocation &&
          google.loader.ClientLocation.address.country_code == "US" &&
          google.loader.ClientLocation.address.region) {
    
        // geo locate was successful and user is in the United States. range
        // check the region so that we can safely use it when selecting a
        // state level polygon overlay
        var state = google.loader.ClientLocation.address.region.toUpperCase();
        if (InTheNews.stateNames[state]) {
          this.currentState_ = state;
        }
      }
      this.currentCountry_ = "US";
    }
    

    告诉你你将从中得到什么:

    当填充时, google.loader.ClientLocation 对象是 填充了以下内容 城域级粒度属性:

    • ClientLocation.latitude 提供与客户端IP地址相关的低分辨率纬度

    • ClientLocation.longitude –提供与客户IP地址相关的低分辨率经度

    • ClientLocation.address.city 提供与客户端IP地址关联的城市的名称

    • ClientLocation.address.country 提供与客户端IP地址关联的国家/地区的名称

    • clientlocation.address.country_code提供与客户端IP地址关联的ISO 3166-1国家代码的名称

    • clientlocation.address.region提供与客户端IP地址关联的特定于国家/地区的地区名称

        2
  •  1
  •   Codingday    16 年前

    它们通常使用IP表进行IP解析。 ip2nation 可能会有一些想法

        3
  •  0
  •   GEOCHET S.Lott    16 年前

    你可能想看看 MaxMind's GeoLite IP定位解决方案。开源解决方案。

        4
  •  0
  •   Peter Mortensen icecrime    14 年前

    有一种新的方法,事实上这是找到地理位置的更好的方法,因为浏览器将在本地支持W3CAPI的地理位置…你应该退房 GeoMereLaal .

        5
  •  0
  •   Achilles    14 年前

    http://humbuckercode.co.uk/licks/gems/geoip/ 是一个使用maxmind免费或付费图书馆的简单宝石。

    设置简单,不需要模式更新。