Class: GeoApi::Gaode

Inherits:
Object
  • Object
show all
Defined in:
lib/geo_api/gaode.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Gaode

Returns a new instance of Gaode.



8
9
10
# File 'lib/geo_api/gaode.rb', line 8

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



6
7
8
# File 'lib/geo_api/gaode.rb', line 6

def config
  @config
end

Instance Method Details

#get_coordinate_from_string(location, city = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/geo_api/gaode.rb', line 30

def get_coordinate_from_string(location, city = nil)
  params = { address: location, city: city, key: @config.key }
  result = send_request('geo', params)
  if !result.nil? && result["status"] == "1" && !result["geocodes"].nil? && result["geocodes"].length > 0
    location = result["geocodes"][0]["location"].split(',')
    return { longitude: location[0], latitude: location[1] }
  else
    return nil
  end
end

#get_location_from_coordinate(longitude, latitude) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/geo_api/gaode.rb', line 12

def get_location_from_coordinate(longitude, latitude)
  params = { location: "%s,%s" % [longitude, latitude], key: @config.key }
  result = send_request('regeo', params)
  if !result.nil? && result["status"] == "1" && !result["regeocode"].nil?
    databack = Hash.new
    databack["address"] = result["regeocode"]["formatted_address"]
    databack["province"] = result["regeocode"]["addressComponent"]["province"]
    databack["city"] = result["regeocode"]["addressComponent"]["city"]
    databack["city"] = databack["province"] if databack["city"].nil? || databack["city"].length == 0
    databack["region"] = result["regeocode"]["addressComponent"]["district"]
    databack["detail"] = result["regeocode"]["addressComponent"]["township"]

    return databack
  else
    return nil
  end
end