Module: BubbleWrap::Location

Defined in:
motion/location/location.rb

Defined Under Namespace

Modules: Error

Class Method Summary collapse

Class Method Details

.authorized?Boolean

returns true/false whether services are enabled for the app

Returns:

  • (Boolean)

163
164
165
# File 'motion/location/location.rb', line 163

def authorized?
  [KCLAuthorizationStatusAuthorized, KCLAuthorizationStatusAuthorizedAlways, KCLAuthorizationStatusAuthorizedWhenInUse].include?(CLLocationManager.authorizationStatus)
end

.enabled?Boolean

returns true/false whether services, or limited services, are enabled for the device

Returns:

  • (Boolean)

158
159
160
# File 'motion/location/location.rb', line 158

def enabled?
  CLLocationManager.locationServicesEnabled
end

.error(type) ⇒ Object


167
168
169
170
171
# File 'motion/location/location.rb', line 167

def error(type)
  @callback && @callback.call({ error: type })
  @callback = nil
  self.location_manager.stopUpdatingLocation
end

.get(options = {}, &block) ⇒ Object

Start getting locations } Example BW::Location.get(distance_filter: 10, desired_accuracy: :nearest_ten_meters) do |result|

result[:to].class == CLLocation
result[:from].class == CLLocation
p "Lat #{result[:to].latitude}, Long #{result[:to].longitude}"

end

Parameters:

  • options (Hash) (defaults to: {})

    {

    authorization_type: :always/:when_in_use to trigger the type of authorization you want

    default == uses :always
    

    significant: true/false; whether to listen for significant location changes or

    all location changes (see Apple docs for info); default == false
    

    distance_filter: minimum change in distance to be updated about, in meters;

    default == uses KCLDistanceFilterNone,
    

    desired_accuracy: minimum accuracy for updates to arrive;

    any of :best_for_navigation, :best, :nearest_ten_meters,
    :hundred_meters, :kilometer, or :three_kilometers; default == :best
    

    purpose: string to display when the system asks user for location, retries: if location cant be found. how many errors do we retry; default == 5 calibration: if the OS should display the heading calibration to the user; default == false


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
88
89
90
91
92
93
94
95
96
# File 'motion/location/location.rb', line 58

def get(options = {}, &block)
  @callback = block
  @callback.weak! if @callback && BubbleWrap.use_weak_callbacks?
  @options = {
    authorization_type: :always,
    significant: false,
    distance_filter: KCLDistanceFilterNone,
    desired_accuracy: KCLLocationAccuracyBest,
    retries: 5,
    once: false,
    calibration: false
  }.merge(options)

  @options[:significant] = false if @options[:significant].nil?
  @retries = 0

  if not enabled?
    error(Error::DISABLED) and return
  end

  self.location_manager

  if self.location_manager.respondsToSelector('requestAlwaysAuthorization')
    @options[:authorization_type] == :always ? self.location_manager.requestAlwaysAuthorization : self.location_manager.requestWhenInUseAuthorization
  end


  self.location_manager.distanceFilter = @options[:distance_filter]
  self.location_manager.desiredAccuracy = Constants.get("KCLLocationAccuracy", @options[:desired_accuracy])
  self.location_manager.purpose = @options[:purpose] if @options[:purpose]

  if @options[:significant]
    self.location_manager.startMonitoringSignificantLocationChanges
  elsif @options[:compass]
    self.location_manager.startUpdatingHeading
  else
    self.location_manager.startUpdatingLocation
  end
end

.get_compass(options = {}, &block) ⇒ Object


131
132
133
# File 'motion/location/location.rb', line 131

def get_compass(options = {}, &block)
  get(options.merge(compass: true), &block)
end

.get_compass_once(options = {}, &block) ⇒ Object


135
136
137
# File 'motion/location/location.rb', line 135

def get_compass_once(options = {}, &block)
  get_compass(options.merge(once: true), &block)
end

.get_once(options = {}, &block) ⇒ Object

Get the first returned location based on your options } Example BW::Location.get_once(desired_accuracy: :three_kilometers, purpose: 'We need to use your GPS to show you how fun RM is') do |result|

if result.is_a?(CLLocation)
  p "Lat #{result.latitude}, Long #{result.longitude}"
else
  p "ERROR: #{result[:error]"
end

end

Parameters:

  • options (Hash) (defaults to: {})

    {

    significant: true/false; whether to listen for significant location changes or

    all location changes (see Apple docs for info); default == false
    

    distance_filter: minimum change in distance to be updated about, in meters;

    default == uses KCLDistanceFilterNone,
    

    desired_accuracy: minimum accuracy for updates to arrive;

    any of :best_for_navigation, :best, :nearest_ten_meters,
    :hundred_meters, :kilometer, or :three_kilometers; default == :best
    

    purpose: string to display when the system asks user for location, retries: if location cant be found. how many errors do we retry; default == 5


127
128
129
# File 'motion/location/location.rb', line 127

def get_once(options = {}, &block)
  get(options.merge(once: true), &block)
end

.get_significant(options = {}, &block) ⇒ Object


98
99
100
# File 'motion/location/location.rb', line 98

def get_significant(options = {}, &block)
  get(options.merge(significant: true), &block)
end

.location_managerObject


151
152
153
154
155
# File 'motion/location/location.rb', line 151

def location_manager
  @location_manager ||= CLLocationManager.alloc.init
  @location_manager.delegate ||= self
  @location_manager
end

.locationManager(manager, didChangeAuthorizationStatus: status) ⇒ Object

CLLocationManagerDelegate Methods


175
176
177
178
179
180
181
182
183
# File 'motion/location/location.rb', line 175

def locationManager(manager, didUpdateToLocation:newLocation, fromLocation:oldLocation)
  if @options[:once]
    @callback && @callback.call(newLocation)
    @callback = proc { |result| }
    stop
  else
    @callback && @callback.call({to: newLocation, from: oldLocation})
  end
end

.locationManagerShouldDisplayHeadingCalibration(manager) ⇒ Object


232
233
234
# File 'motion/location/location.rb', line 232

def locationManagerShouldDisplayHeadingCalibration(manager)
  @options[:calibration] ? @options[:calibration] : false
end

.stopObject

Stop getting locations


140
141
142
143
144
145
146
147
148
149
# File 'motion/location/location.rb', line 140

def stop
  return unless @options
  if @options[:significant]
    self.location_manager.stopMonitoringSignificantLocationChanges
  elsif @options[:compass]
    self.location_manager.stopUpdatingHeading
  else
    self.location_manager.stopUpdatingLocation
  end
end