Class: GeoApi::LocationService

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/geo_api/location_service.rb

Instance Method Summary collapse

Instance Method Details

#get_coordinate_from_string(location, city = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/geo_api/location_service.rb', line 55

def get_coordinate_from_string(location, city = nil)
  params = { address: location, region: city, key: GeoApi.config.key }
  result = send_request(params)

  if result["status"] == 0
    return result["result"]
  else
    return nil
  end
end

#get_location_from_coordinate(latitude, longitude, coordtype = 5) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/geo_api/location_service.rb', line 36

def get_location_from_coordinate(latitude, longitude, coordtype = 5)
  params = { location: "%s,%s" % [latitude, longitude], coordtype: coordtype, key: GeoApi.config.key }
  result = send_request(params)
  if result && result["status"] == 0 && result["result"]
    databack = Hash.new
    databack["address"] = result["result"]["address"]
    databack["province"] = result["result"]["address_component"]["province"]
    databack["city"] = result["result"]["address_component"]["city"]
    databack["region"] = result["result"]["address_component"]["district"]
    databack["detail"] = result["result"]["address_component"]["street"] + result["result"]["address_component"]["street_number"]
    databack["latitude"] = result["result"]["location"]["lat"].to_s
    databack["longitude"] = result["result"]["location"]["lng"].to_s

    return databack
  else
    return nil
  end
end

#get_location_from_string(location) ⇒ Object



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
# File 'lib/geo_api/location_service.rb', line 10

def get_location_from_string(location)
  #params = { address: location }
  #result = send_request(params)

  unless location.blank?
    
    formated_address = location.split(/,|-|;|>|:|\+|\^/)
    databack = Hash.new
    databack["province"] = formated_address[0] if formated_address.length > 0
    databack["city"] = formated_address[1] if formated_address.length > 1
    databack["region"] = formated_address[2] if formated_address.length > 2
    databack["detail"] = formated_address[3] if formated_address.length > 3
    databack["latitude"] = ""
    databack["longitude"] = ""

    if ["重庆市", "上海市", "北京市", "天津市"].include?(databack["province"])
      databack["region"] = databack["city"]
      databack["city"] = databack["province"]
    end

    return databack
  else
    return nil
  end
end