Class: BaiduGeocoder::Geocoder
- Inherits:
-
Object
- Object
- BaiduGeocoder::Geocoder
- Defined in:
- lib/baidu_geocoder.rb
Constant Summary collapse
- ApiHost =
"http://api.map.baidu.com"- ApiAction =
"/geocoder/v2/"- ApiUrl =
ApiHost + ApiAction
- DefaultAK =
"LDw1AMLeanI9hEMjKG4DCzczYVaxAYFC"- DefaultLocation =
{:lng => 116.421885, :lat => 39.938574, :status => false}.freeze
- DefaultAddress =
{:province => "北京市", :city => "市辖区", :district => "东城区", :formatted_address => "北京市市辖区东城区", :status => false}.freeze
Class Method Summary collapse
-
.get_address_by_loc(lat, lng, ak = DefaultAK, http = HTTP) ⇒ Object
get address by location(latitude, longtitude).
- .get_address_by_name(name, ak = DefaultAK) ⇒ Object
-
.get_location_by_addr(addr, city = nil, ak = DefaultAK, http = HTTP) ⇒ Object
get location by address.
Class Method Details
.get_address_by_loc(lat, lng, ak = DefaultAK, http = HTTP) ⇒ Object
get address by location(latitude, longtitude)
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# 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) ⇒ Object
67 68 69 70 71 72 |
# 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) ⇒ Object
get location by address
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# 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 |