Class: GoogleMaps::Services::Geolocation

Inherits:
Object
  • Object
show all
Includes:
Exceptions
Defined in:
lib/googlemaps/services/geolocation.rb

Overview

Performs requests to the Google Geolocation API.

Examples:

geolocation = GoogleMaps::Services::Geolocation.new(client)
location = geolocation.query(home_mobile_country_code: "310",
                        home_mobile_network_code: "410",
                        radio_type: "gsm",
                        carrier: "Vodafone",
                        consider_ip: "true")
# {"location"=>{"lat"=>51.021327, "lng"=>3.7070152}, "accuracy"=>2598.0}

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Geolocation

Returns a new instance of Geolocation.

Since:

  • 1.0.0



24
25
26
# File 'lib/googlemaps/services/geolocation.rb', line 24

def initialize(client)
  self.client = client
end

Instance Attribute Details

#clientSymbol

Returns The HTTP client.

Returns:

  • (Symbol)

    The HTTP client.

Since:

  • 1.0.0



22
23
24
# File 'lib/googlemaps/services/geolocation.rb', line 22

def client
  @client
end

Instance Method Details

#query(home_mobile_country_code: nil, home_mobile_network_code: nil, radio_type: nil, carrier: nil, consider_ip: true, cell_towers: nil, wifi_access_points: nil) ⇒ HashMap

The Google Maps Geolocation API returns a location and accuracy radius based on information about cell towers and given WiFi nodes.

For more information, see: developers.google.com/maps/documentation/geolocation/intro

Parameters:

  • home_mobile_country_code (String) (defaults to: nil)

    The mobile country code (MCC) for the device’s home network.

  • home_mobile_network_code (String) (defaults to: nil)

    The mobile network code (MNC) for the device’s home network.

  • radio_type (String) (defaults to: nil)

    The mobile radio type. Supported values are lte, gsm, cdma, and wcdma. While this field is optional, it should be included if a value is available, for more accurate results.

  • carrier (String) (defaults to: nil)

    The carrier name.

  • consider_ip (TrueClass, FalseClass) (defaults to: true)

    Specifies whether to fall back to IP geolocation if wifi and cell tower signals are not available. Note that the IP address in the request header may not be the IP of the device.

  • cell_towers (Array) (defaults to: nil)
  • wifi_access_points (Array) (defaults to: nil)

Returns:

  • (HashMap)

    The location with accuracy radius.

Since:

  • 1.0.0



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
# File 'lib/googlemaps/services/geolocation.rb', line 47

def query(home_mobile_country_code: nil, home_mobile_network_code: nil, radio_type: nil,
          carrier: nil, consider_ip: true, cell_towers: nil, wifi_access_points: nil)
  params = {}

  if home_mobile_country_code
    params["homeMobileCountryCode"] = home_mobile_country_code
  end

  if home_mobile_network_code
    params["homeMobileNetworkCode"] = home_mobile_network_code
  end

  if radio_type
    raise StandardError, "invalid radio type value." unless Constants::SUPPORTED_RADIO_TYPES.include? radio_type
    params["radioType"] = radio_type
  end

  if carrier
    params["carrier"] = carrier
  end

  params["considerIp"] = consider_ip unless consider_ip


  if cell_towers
    params["cellTowers"] = cell_towers
  end

  if wifi_access_points
    params["wifiAccessPoints"] = wifi_access_points
  end

  self.client.request(url: '/geolocation/v1/geolocate',
                      params: {},
                      base_url: Constants::GOOGLEAPIS_BASE_URL,
                      extract_body: lambda(&method(:_geolocation_extract)),
                      post_json: params)
end