class BaiduGeocoder::Geocoder
Constants
- ApiAction
- ApiHost
- ApiUrl
- DefaultAK
- DefaultAddress
- DefaultLocation
Public Class Methods
get_address_by_loc(lat, lng, ak = DefaultAK, http = HTTP)
click to toggle source
get address by location(latitude, longtitude)
# File lib/baidu_geocoder.rb, line 42 def get_address_by_loc(lat, lng, ak = DefaultAK, http = HTTP) params = {:location => "#{lat},#{lng}"}.merge({:output => "json", :ak => ak}) address = {}.merge(DefaultAddress) begin res = http.get(ApiUrl, :params => params).flush if res.code == 200 json = JSON.parse(res.body) if "0".eql?(json["status"].to_s) && json["result"] && !json["result"].empty? \ && json["result"]["addressComponent"] && !json["result"]["addressComponent"].empty? \ && json["result"]["formatted_address"] && !json["result"]["formatted_address"].empty? address[:formatted_address] = json["result"]["formatted_address"] address[:province] = json["result"]["addressComponent"]["province"] address[:city] = json["result"]["addressComponent"]["city"] address[:district] = json["result"]["addressComponent"]["district"] address[:status] = true end end rescue Exception => e puts "Exception => #{e}" end address end
get_address_by_name(name, ak = DefaultAK)
click to toggle source
# File lib/baidu_geocoder.rb, line 67 def get_address_by_name name, ak = DefaultAK loc = get_location_by_addr(name, ak) address = get_address_by_loc(loc[:lat], loc[:lng], ak) address[:status] = loc[:status] && address[:status] address end
get_location_by_addr(addr,city = nil, ak = DefaultAK, http = HTTP)
click to toggle source
get location by address
# File lib/baidu_geocoder.rb, line 16 def get_location_by_addr(addr,city = nil, ak = DefaultAK, http = HTTP) names = {'宁夏' => '宁夏回族自治区', '新疆'=> '新疆维吾尔自治区', '西藏'=> '西藏自治区', '广西'=> '广西壮族自治区', '内蒙'=> '内蒙古自治区'} names.each do |k,v| addr.gsub!(/#{k}/, v) end params = {:address => addr, :city => city}.merge({:output => "json", :ak => ak}) loc = {}.merge(DefaultLocation) begin res = http.get(ApiUrl, :params => params).flush if res.code == 200 json = JSON.parse(res.body) if "0".eql?(json["status"].to_s) && json["result"] && !json["result"].empty? \ && json["result"]["location"] && !json["result"]["location"] .empty? loc[:lng] = json["result"]["location"]["lng"] loc[:lat] = json["result"]["location"]["lat"] loc[:status] = true end end rescue Exception => e puts "Exception=> #{e}" end loc end