Class: GitFit::Geo::AMapClient

Inherits:
Object
  • Object
show all
Defined in:
lib/git_fit/geo/amap_client.rb

Constant Summary collapse

BASE_URL =
"https://restapi.amap.com/v3/geocode/regeo".freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_key:) ⇒ AMapClient

Returns a new instance of AMapClient.



10
11
12
13
14
15
# File 'lib/git_fit/geo/amap_client.rb', line 10

def initialize(api_key:)
  @api_key = api_key
  @mutex = Mutex.new
  @last_request = 0.0
  @min_interval = 0.2
end

Instance Method Details

#reverse_geocode(lat, lng) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/git_fit/geo/amap_client.rb', line 17

def reverse_geocode(lat, lng)
  rate_limit
  uri = URI(BASE_URL)
  uri.query = URI.encode_www_form(
    key: @api_key,
    location: "#{lng},#{lat}",
    output: "JSON",
    radius: 1000,
    extensions: "all",
  )
  resp = Net::HTTP.get_response(uri)
  body = JSON.parse(resp.body)
  if body["status"] != "1"
    raise "AMap API error: #{body["info"]}"
  end
  parse_response(body)
end