File: amap.rb

package info (click to toggle)
ruby-geocoder 1.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 732 kB
  • sloc: ruby: 6,173; makefile: 3
file content (87 lines) | stat: -rw-r--r-- 1,772 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require 'geocoder/results/base'

module Geocoder::Result
  class Amap < Base

    def coordinates
      location = @data['location'] || @data['roadinters'].try(:first).try(:[], 'location') \
        || address_components.try(:[], 'streetNumber').try(:[], 'location')
      location.to_s.split(",").reverse.map(&:to_f)
    end

    def address
      formatted_address
    end

    def state
      province
    end

    def province
      address_components['province']
    end

    def city
      address_components['city'] == [] ? province : address_components["city"]
    end

    def district
      address_components['district']
    end

    def street
      if address_components["neighborhood"]["name"] != []
        return address_components["neighborhood"]["name"]
      elsif address_components['township'] != []
        return address_components["township"]
      else
        return @data['street'] || address_components['streetNumber'].try(:[], 'street')
      end
    end

    def street_number
      @data['number'] || address_components['streetNumber'].try(:[], 'number')
    end

    def formatted_address
      @data['formatted_address']
    end

    def address_components
      @data['addressComponent'] || @data
    end

    def state_code
      ""
    end

    def postal_code
      ""
    end

    def country
      "China"
    end

    def country_code
      "CN"
    end

    ##
    # Get address components of a given type. Valid types are defined in
    # Baidu's Geocoding API documentation and include (among others):
    #
    #   :business
    #   :cityCode
    #
    def self.response_attributes
      %w[roads pois roadinters]
    end

    response_attributes.each do |a|
      define_method a do
        @data[a]
      end
    end
  end
end