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

Cod*_*nts 11 ruby language-agnostic location ruby-on-rails detect

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

知道我可以在用户首次登录网站时询问用户所在的位置,但我注意到许多网站都内置了此功能以自动检测位置(例如,请参阅Last.fm "事件:您的音乐会列表区域").

他们如何做到这一点?我目前正在使用Ruby on Rails构建我的网站.

run*_*ako 9

以下是相关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";
}
Run Code Online (Sandbox Code Playgroud)

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

填充后,将 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地址关联的国家/地区特定区域名称


Cod*_*day 1

他们通常通过 IP 表使用 IP 解析。 ip2nation可能会提供一些想法