2009-02-27 52 views
11

我目前正在展示列表网站上工作。我将按位置显示用户以各种不同方式排序的显示信息。如何自动查找用户的位置?

知道我可以问当他们第一次登录到该网站,他们所在的用户,但我注意到,很多网站都这个功能内置自动检测位置(例如,见Last.fm“活动:您所在地区的音乐会清单“)。

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

+0

不要忘记上传你喜欢的一些答案,并可能标记一个被接受的答案。人们总是可以使用鼓励。 – GEOCHET 2009-02-27 22:58:40

+0

我upvoted三个答案。但有人一直投下一切。 – CodingWithoutComments 2009-02-27 22:59:41

+0

@编码:有趣。继续,很好的问题。 – GEOCHET 2009-02-27 23:01:50

回答

9

下面是对文件的链接在相关谷歌地图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 - 提供名称th与客户端的IP地址

  • ClientLocation.address.country相关的E都市 - 提供与客户端的IP地址

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

  • ClientLocation.address.region - 提供与客户端的IP地址

012相关联的国家特定区域名称
0

您可能会喜欢查看MaxMind's GeoLite解决方案的IP位置。开源解决方案。

0

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