Rails 如何实现通过登录IP确定城市功能
Rails 如何实现通过登录IP确定城市功能
Rails 如何实现通过登录IP确定城市功能
博客分类:RubyOnRails
对于Rails而言,主流方式应该是使用google库的插件geoip
github地址如下
- 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"]
另外一个 geo_ip
使用如下:
- 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"
- }
geokit是一个关于地理的工具,比如根据经纬度确定城市和距离之类
- #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很多比如网易
http://www.youdao.com/smartresult-xml/search.s?type=ip&q=IP地址
- 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
- 01:17
- 评论 / 浏览 (2 / 466)
- 分类:编程语言
- 相关推荐
评论
转载于:https://www.cnblogs.com/lexus/archive/2011/08/19/2146342.html
Rails 如何实现通过登录IP确定城市功能相关推荐
- 利用百度地图API进行根据ip定位城市
根据ip进行定位城市,本次利用的是根据百度地图API进行定位.网上也有其他的方法.本次就将我自己写的东西总结一下: 一.首先获取ip地址 public static String getReqIp(H ...
- php免费根据ip查城市,根据ip获取城市的方法
这个方法放到includes/lib_base.php的最下面 /* * * 根据ip获取城市的方法 * @param type $ip ip地址 * @return boolean|string * ...
- python 通过ip获取城市_python中通过客户端IP拿到所在城市和当地天气信息—附带项目案例...
首先,需求分析: 1.拿到客户端IP 2.通过IP拿到客户端所在地区 3.通过地区拿到当地天气 4.整合功能,展示给用户 第一步,如何拿到用户IP 我们以Django环境为例 # 客户端的请求,IP信 ...
- 根据Ip获取城市帮助类
思路构建 1.先通过本地的测IP地址库进行匹配 2.如果本地IP地址库存在此IP的城市信息,就直接返回,调用速度也快 3.如果本地没有对应的IP城市信息,必须通过调用网络的IP查询的API了,这里我使 ...
- python 通过ip获取城市_Python根据用户IP判断所属城市 !
事情源于7月以来,SEO销售额下降比较明显,老板让我想想办法提升SEO销售额. 而销量=流量×转化率×客单价,客单价我这边没法控制,只能从流量和转化率两个方向入手了.但对SEO来讲,短时间大幅度提升S ...
- 连接思科无线经常出现获取不到地址_思科(cisco)路由器登录IP地址默认密码说明...
思科路由器登录IP地址说明: 要登录和配置思科路由器,我们需要先知道两件事. 1.思科路由器的登录IP地址 2.思科路由器默认登录用户名和密码 本指南说明将为你带来两种设备如何登录和配置思科路由器 步 ...
- 如何知晓代理IP所属城市?
拿到一个IP之后,如何确定这个IP是属于哪个城市的呢,方法比较多,这里介绍两个最方便的方法. 第一,直接百度搜索这个IP地址,比如IP地址是127.0.0.1,那么你百度搜索127.0.0.1就会出现 ...
- 根据IP获取城市-新浪ip接口
/**+----------------------------------------------------------* content : 获取IP地址* time : 2018年7月9日+- ...
- Android 通过外网IP定位城市
Android 通过外网IP定位城市 获取外网IP /*** 通过CmyIP获取获取外网外网地址 需在异步线程中访问* @return 外网IP*/ public static String getO ...
最新文章
- 拥抱.NET Core,如何开发一个跨平台类库
- 基于应用日志的扫描器检测实践
- ODP.net与Oracle连接
- 线性规划 - 用单纯形法解决LP问题 - (Matlab、Lingo建模)
- spring学习笔记05-IOC常用注解(二)
- 电脑温度检测软件哪个好_重装电脑,用哪个软件重装系统比较好?
- The7主题-汉化绿色版/免key导入demo/安装插件[更至v9.10.1]
- python使用工具简介介绍
- 17 类的成员 私有
- Linux使用Blowfish生成密码,linux批量生成密码
- html5离散数学,离散数学怎样判断合取范式和析取范式
- 中国社交产品十年记....
- element遮罩_element-ui遮罩层el-dialog的使用
- LVGL:模拟器仿真
- 系统问题导致的笔记本部分按键失灵
- zkSnark教程:从方程到验证
- 13:js逆向-登录加密(aes加密)
- AJ-Report小白配置大屏手册
- 分支限界——TSP问题
- 2020年T电梯修理考试资料及T电梯修理考试技巧