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


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

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

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


9879
2018-02-27 22:28


起源

不要忘记赞成你喜欢的一些答案,并可能标记一个接受的答案。人们总是可以使用鼓励。 - GEOCHET
我赞成了三个答案。但有人一直在投票。 - CodingWithoutComments
@Coding:有趣。继续,好问题。 - GEOCHET


答案:


以下是相关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地址关联的国家/地区特定区域名称


9
2018-02-27 22:39



这很酷,谢谢。 - CodingWithoutComments
我对社区有点新意,所以这可能不合适,但是如何从页面中包含一个片段实质上增加了答案?他仍然需要点击链接才能使用API​​,不是吗? - runako
@runako:想法是SO是一个充满信息的wiki。所以拥有这个链接是一个很好的开始,但提供更多信息以便让人们有一个良好的开端甚至更好。 - GEOCHET
@runako - 如果页面移动或被删除怎么办? - ceejayoz
在这种情况下,上面发布的文档也很可能已被弃用。 - runako


它们通常使用IP表的IP解析。 ip2nation 可能会给出一些想法


1
2018-02-27 22:32





你可能想看看 MaxMind的GeoLite IP位置的解决方案。一个开源解决方案。


0
2018-02-27 22:40



甜。非常感谢。 - CodingWithoutComments


有一种新方法,实际上它是找到地理位置的更好方法,因为浏览器本身支持W3C API进行地理定位...你应该看看 GeoMereLaal


0
2017-10-27 12:57





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

易于设置,无需更新架构。


0
2018-03-09 17:07