`
jn615
  • 浏览: 98546 次
  • 性别: Icon_minigender_1
  • 来自: 火星
社区版块
存档分类
最新评论

ruby 实现 bing 的 geocode

阅读更多
项目中需要进行geo_code
刚开始用的是geokit的gem, 支持多个api, google和yahoo 但是没有bing的, 客户又要求使用bing的

没办法, 搜, geocoder(https://github.com/alexreisner/geocoder)中支持bing的, 但是从rails3良好支持, rails2 的分支中好像没提到,无奈项目又是rails2.3.8的, 所以我把geo_coder源码中的bing部分抠了下来

注: 先去bing申请api key  www.bingmapsportal.com

require 'net/http'
require 'uri'
require 'rubygems'
require 'json'

class Address

  def geocode(address)
    results(address)
  end

  
  def results(query)
    return [] unless doc = fetch_data(query)
    if doc['statusDescription'] == "OK"
      return doc['resourceSets'].first['estimatedTotal'] > 0 ? doc['resourceSets'].first['resources'] : []
    else
      warn "Bing Geocoding API error: #{doc['statusCode']} (#{doc['statusDescription']})."
      return []
    end
  end

  def query_url(query)
    params = {:key => "API-Key"}
    params[:query] = query

    base_url = "http://dev.virtualearth.net/REST/v1/Locations"
    url_tail = "?"
    base_url + url_tail + hash_to_query(params)
  end

  def hash_to_query(hash)
    require 'cgi' unless defined?(CGI) && defined?(CGI.escape)
    hash.collect{ |p|
      p[1].nil? ? nil : p.map{ |i| CGI.escape i.to_s } * '='
    }.compact.sort * '&'
  end
  
  def fetch_data(query)
    begin
      parse_raw_data fetch_raw_data(query)
    rescue SocketError => err
      raise_error(err) or warn "Geocoding API connection cannot be established."
    rescue TimeoutError => err
      raise_error(err) or warn "Geocoding API not responding fast enough " +
        "(see Geocoder::Configuration.timeout to set limit)."
    end
  end

  ##
  # Fetches a raw search result (JSON string).
  #
  def fetch_raw_data(query)
    timeout(30) do
      url = query_url(query)
      uri = URI.parse(url)
      client = http_client.new(uri.host, uri.port)
      response = client.get(uri.request_uri).body
      response
    end
  end

  ##
  # Parses a raw search result (returns hash or array).
  #
  def parse_raw_data(raw_data)
    if defined?(ActiveSupport::JSON)
      ActiveSupport::JSON.decode(raw_data)
    else
      begin
        JSON.parse(raw_data)
      rescue
        warn "Geocoding API's response was not valid JSON."
      end
    end
  end

  ##
  # Object used to make HTTP requests.
  #
  def http_client
    Net::HTTP
  end
end


address = Address.new
address.geocode("25 Main St, Cooperstown, NY")
分享到:
评论

相关推荐

    ArcGIS for android之Geocode实现(配套工程)

    本文将详细讲解如何在Android应用中利用ArcGIS SDK实现Geocode功能,即通过地址文本获取坐标位置的过程。这个"Geocode"项目就是针对这个主题的一个配套工程,用于帮助开发者更直观地理解和实践。 首先,我们需要...

    Ruby-使用百度地图API进行地址定位坐标坐标定位地址名称定位地址

    在Ruby开发中,有时我们需要将地址转换为经纬度坐标,或者根据坐标反向查询地址,这时可以利用百度地图API来实现。本篇文章将详细介绍如何在Ruby中使用百度地图API进行地址定位、坐标定位以及名称定位。 首先,我们...

    matlab开发-GeoCode

    MATLAB中的GeoCode功能可能通过API接口与第三方地理编码服务提供商(如Google Maps、Bing Maps、OpenStreetMap等)进行通信。开发者通常需要注册并获得API密钥来合法地使用这些服务。在`geoCode.m`这个MATLAB脚本中...

    arcgis server geocode java

    描述提到这是一个基于ArcGIS Server实现的geocode实例,这可能包括以下几个方面: 1. **服务配置**:首先,需要在ArcGIS Server Manager中创建和配置一个地理编码服务。这通常涉及加载包含地址数据的图层,设置匹配...

    Laravel开发-geocode

    标题提到的"Laravel开发-geocode"是一个针对Laravel 4的扩展,它允许开发者集成Google Geocoding API,以便在应用中实现地理定位功能。这个扩展是基于`jcf/geocode`项目进行的更新和维护,以适应Guzzle库的新版本。 ...

    geocode

    GeoCode支架式可视化编程平台 关于 GeoCode的目标是通过编程实践让学生探索地质威胁。 在这种情况下,我们使用Blockly生成运行简单的tephra分发模型的代码。 学生使用更改喷发参数。 链接和分支 master :最新作品...

    Ruby-GeokitGeokitgem提供了地理编码和距离计算

    Ruby-Geokit是一个强大的开源库,专为Ruby开发者设计,用于处理与地理位置相关的任务,如地理编码、反向地理编码以及进行距离和方向的计算。这个gem极大地简化了在Ruby应用中集成地理服务的工作,使得开发者能够轻松...

    PyPI 官网下载 | appel_geocode-0.1.1-py3-none-any.whl

    《PyPI官网下载:appel_geocode-0.1.1-py3-none-any.whl——深入了解Python库的发布与安装》 PyPI(Python Package Index),是Python开发者的重要资源库,它为全球的Python开发者提供了无数的第三方库,使得开发...

    Google-Maps-Geocode-Api

    Google Maps Geocode API Google Maps Geocode API的简单PHP包装器 安装及要求 首先,您需要使用Composer来打包: $ composer require marcoboom/google-maps-geocode-api 该软件包需要PHP 5.4和 用法 $ geo =...

    Laravel开发-geocode .zip.zip

    在本项目中,我们关注的是"Laravel开发-geocode .zip.zip",这似乎是一个包含Laravel框架下关于地理编码(Geocoding)开发的资源包。由于标签部分为空,我们将主要依据标题和描述来推测内容。从标题来看,我们可以...

    geocode-glib-3.26.0-3.el8.ppc64le.rpm

    离线安装包,亲测可用

    react-geocode-earth-autocomplete:geocode.earth自动完成的React组件

    一个React组件,用于为geocode.earth构建自定义的UI 使您能够轻松构建由提供支持的自定义自动完成下拉列表。 完全控制渲染,以便与其他库(例如Redux-Form)很好地集成。 基本上是的geocode.earth版本。 安装 ...

    reverse_geocode

    >> > import reverse_geocode >> > coordinates = ( - 37.81 , 144.96 ), ( 31.76 , 35.21 ) >> > reverse_geocode . search ( coordinates ) [{ 'city' : 'Melbourne' , 'country_code' : 'AU' , 'country' : '...

    geocode:将国家列表转换为几何位置

    运行以下命令: python geocode.py -i example/countries.txt -k YOUR_API_KEY_HERE请注意,较长的国家/地区列表将需要一些时间来处理。 完成后,国家/地区将打印到控制台,并将JSON结果写入输出文件。例子使用在...

    geocodejson:GeocodeJSON格式和Geocode数据提供程序的接口和实用程序

    GeocodeJSON GeocodeJSON软件包是一个小型实用程序的集合,可与地理编码API(例如Google Geocoding API , HERE Geocoding API , Opencage等)以及自托管解决方案(如。 这些程序包提供了一个简单的... geocode :使

    bangladesh-geocode, 分区,区域,Upazila和联合级别完全映射的MySQL数据库.zip

    bangladesh-geocode, 分区,区域,Upazila和联合级别完全映射的MySQL数据库 GeoCode分,区,Upazila,联合级完全映射MySQL数据库。 导入数据库中的所有文件并使 Join 查询( 可能是) 获得所需的地理信息。所有的信息...

Global site tag (gtag.js) - Google Analytics