Rails 如何实现通过登录IP确定城市功能

对于Rails而言,主流方式应该是使用google库的插件geoip
[url=http://github.com/simplificator/geoip]github地址如下[/url]

  require 'geoip'
GeoIP.new('GeoLiteCity.dat').country('www.atlantis.sk')
=> ["www.atlantis.sk", "217.67.18.26", "SK", "SVK", "Slovakia", "EU", "02", "Bratislava", "", 48.15, 17.1167, nil, nil, "Europe/Bratislava"]

Returned values are the requested hostname, the IP address as a dotted quad,
Maxmind's country code, the ISO3166-1 country code, the ISO3166-2 country code,
the ISO3166 country name, and the continent code.

GeoIP.new('GeoCity.dat').city('github.com')
=> ["github.com", "207.97.227.239", "US", "USA", "United States", "NA", "CA", "San Francisco", "94110", 37.7484, -122.4156, 807, 415, "America/Los_Angeles"]

Returned values are the country values followed by region or state name,
city name, postal_code/zipcode, latitude, longitude, USA DMA code, USA area code,
timezone name. Sorry it's not a Hash... historical.

GeoIP.new('GeoIPASNum.dat').asn("www.fsb.ru")
=> ["AS8342", "RTComm.RU Autonomous System"]


另外一个 [url=http://github.com/jeroenj/geo_ip]geo_ip[/url]

使用如下:
  GeoIp.geolocation(ip_address)


  # 209.85.227.104 = google.be (US)
GeoIp.geolocation('209.85.227.104')
#returns:

{
:status =>"OK",
:ip =>"209.85.227.104"
:country_code =>"US",
:country_name =>"United States",
:region_code =>"06",
:region_name =>"California",
:city =>"Mountain View",
:zip_postal_code =>"94043",
:latitude =>"37.4192",
:longitude =>"-122.057"
}


[url=git://github.com/andre/geokit-rails.git]geokit[/url]是一个关于地理的工具,比如根据经纬度确定城市和距离之类

#Find near latitude and longitude:
Store.find(:all, :origin =>[37.792,-122.393], :within=>10)
#Find near an address:
Store.find(:all, :origin=>'100 Spear st, San Francisco, CA', :within=>10)
#Order by distance from the center of a zipcode:
Store.find(:all, :origin=>'94117', :within=>10,
:order=>'distance asc')
#Combine distance conditions with regular conditions
Store.find(:all, :origin=>'94117', :within=>10,
:conditions=>{:store_type=>'CAFE'})



一个是通过网络的IP查询API,这个办法IP库更新比较快。通用的库有几个比如google。
xml处理页面完全可以通过nokogiri等专门处理工具代替

提供IP地址查询的API很多比如网易
[url]http://www.youdao.com/smartresult-xml/search.s?type=ip&q=IP地址[/url]

require 'net/http'
require 'rexml/document'
include REXML

class MapsController < ApplicationController
def index
@location = locateIp()
end

def locateIp
#ip = "123.123.123.123";
ip = request.remote_ip
ips = ip.to_s
url = "http://ipinfodb.com/ip_query.php?ip="+ips+"&timezone=false"

xml_data = Net::HTTP.get_response(URI.parse(url)).body

xmldoc = REXML::Document.new(xml_data)

# Now get the root element
root = xmldoc.root
city = ""
regionName = ""
countryName = ""

# This will take country name...
xmldoc.elements.each("Response/CountryName") {
|e| countryName << e.text
}

# Now get city name...
xmldoc.elements.each("Response/City") {
|e| city << e.text
}

# This will take regionName...
xmldoc.elements.each("Response/RegionName") {
|e| regionName << e.text
}

ipLocation = city +", "+regionName+", "+countryName

return ipLocation

end #end of method locateIp

end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值