我目前正在制作一个节目列表网站。我将按位置显示用户以各种不同方式排序的节目信息。
一世 知道 我可以在用户首次登录网站时询问用户所在位置,但我注意到许多网站都内置了此功能以自动检测位置(例如,请参阅 Last.fm “活动:您所在地区的音乐会列表”)。
他们如何做到这一点?我目前正在使用Ruby on Rails构建我的网站。
我目前正在制作一个节目列表网站。我将按位置显示用户以各种不同方式排序的节目信息。
一世 知道 我可以在用户首次登录网站时询问用户所在位置,但我注意到许多网站都内置了此功能以自动检测位置(例如,请参阅 Last.fm “活动:您所在地区的音乐会列表”)。
他们如何做到这一点?我目前正在使用Ruby on Rails构建我的网站。
以下是相关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地址关联的国家/地区特定区域名称
它们通常使用IP表的IP解析。 ip2nation 可能会给出一些想法
你可能想看看 MaxMind的GeoLite IP位置的解决方案。一个开源解决方案。
有一种新方法,实际上它是找到地理位置的更好方法,因为浏览器本身支持W3C API进行地理定位...你应该看看 GeoMereLaal。
http://humbuckercode.co.uk/licks/gems/geoip/ 是一个使用Maxminds免费或付费库的简单宝石。
易于设置,无需更新架构。