Class: Baidu::Map

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/baidu/map.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Map

Returns a new instance of Map.



5
6
7
8
# File 'lib/baidu/map.rb', line 5

def initialize(key)
  @key = key
  @segments = {:ak=>@key,:output=>'json',:radius=>1000}
end

Class Method Details

.coordinate(poiname, cityid) ⇒ Object

从每条结果中解析x,y坐标



47
48
49
50
51
52
53
54
55
# File 'lib/baidu/map.rb', line 47

def self.coordinate(poiname,cityid)
  # puts "http://api.map.baidu.com/geocoder?address=#{URI.encode(poiname)}&output=json&key=#{@key}&city=#{URI.encode(cityid)}"
  url = "http://api.map.baidu.com/geocoder?address=#{URI.encode(poiname)}&output=json&key=#{@key}&city=#{cityid}"
  result = get url
  location = result['result']
  return nil if location.nil? or location.empty?
  location = location['location']
  return location['lng'],location['lat'] if result['status'] == 'OK'
end

.get_cityid(cityname) ⇒ Object

得到百度地图城市id



41
42
43
44
# File 'lib/baidu/map.rb', line 41

def self.get_cityid(cityname)
  body = get "http://api.map.baidu.com/?qt=cur&wd=#{URI.encode(cityname)}&ie=utf-8&res=api"
  return body['content']['code']
end

Instance Method Details

#around(lat, lng) ⇒ Object



30
31
32
33
# File 'lib/baidu/map.rb', line 30

def around(lat,lng)
  @segments[:location] = "#{lat},#{lng}"
  self
end

#bankObject



15
16
17
18
# File 'lib/baidu/map.rb', line 15

def bank
  @segments[:query]='银行'
  self
end

#busObject



10
11
12
13
# File 'lib/baidu/map.rb', line 10

def bus
  @segments[:query]='公交站'
  self
end

#cafeObject



20
21
22
23
# File 'lib/baidu/map.rb', line 20

def cafe
  @segments[:query]='餐馆'
  self
end

#infoObject



57
58
59
60
61
62
63
64
# File 'lib/baidu/map.rb', line 57

def info
  uri = URI::HTTP.build(
    :host => 'api.map.baidu.com',
    :query=> URI.encode(@segments.map{|k,v|"#{k}=#{v}"}.join('&')),
    :path => '/place/v2/search'
  )
  self.class.get(uri.to_s)
end

#spotObject



25
26
27
28
# File 'lib/baidu/map.rb', line 25

def spot
  @segments[:query]='景点'
  self
end

#within(radius) ⇒ Object



35
36
37
38
# File 'lib/baidu/map.rb', line 35

def within(radius)
  @segments[:radius] = radius
  self
end