Module: BubbleWrap::Location

Defined in:
motion/location/location.rb

Defined Under Namespace

Modules: Error

Class Method Summary collapse

Class Method Details

.const_int_get(base, value) ⇒ Object



138
139
140
141
142
# File 'motion/location/location.rb', line 138

def const_int_get(base, value)
  return value if value.is_a? Numeric
  value = value.to_s.camelize
  Kernel.const_get("#{base}#{value}")
end

.enabled?Boolean

returns true/false whether services are enabled for the device

Returns:

  • (Boolean)


92
93
94
# File 'motion/location/location.rb', line 92

def enabled?
  CLLocationManager.locationServicesEnabled
end

.error(type) ⇒ Object



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

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: {})

    {

    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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'motion/location/location.rb', line 47

def get(options = {}, &block)
  @callback = block
  @options = options

  @options[:significant] = false if @options[:significant].nil?
  @options[:distance_filter] ||= KCLDistanceFilterNone
  @options[:desired_accuracy] ||= KCLLocationAccuracyBest
  @options[:retries] ||= 5
  @retries = 0

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

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

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

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



72
73
74
# File 'motion/location/location.rb', line 72

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

.load_constants_hackObject



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

def load_constants_hack
  [KCLLocationAccuracyBestForNavigation, KCLLocationAccuracyBest,
    KCLLocationAccuracyNearestTenMeters, KCLLocationAccuracyHundredMeters,
    KCLLocationAccuracyKilometer, KCLLocationAccuracyThreeKilometers,
  ]
end

.location_managerObject



85
86
87
88
89
# File 'motion/location/location.rb', line 85

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

.locationManager(manager, didChangeAuthorizationStatus: status) ⇒ Object

CLLocationManagerDelegate Methods



104
105
106
# File 'motion/location/location.rb', line 104

def locationManager(manager, didUpdateToLocation:newLocation, fromLocation:oldLocation)
  @callback.call({to: newLocation, from: oldLocation})
end

.stopObject

Stop getting locations



77
78
79
80
81
82
83
# File 'motion/location/location.rb', line 77

def stop
  if @options[:significant]
    self.location_manager.stopMonitoringSignificantLocationChanges
  else
    self.location_manager.stopUpdatingLocation
  end
end