-->

Rubymotion / bubblewrap geolocation in airplane mo

2019-04-17 06:19发布

问题:

Trying to get an clean geoloc implementation in some app; quite struggling with the limitations of what bubblewrap provide for locations

   def get_location
        begin
          BW::Location.get_once do |location|
            if location
              self.latitude   = location.latitude
              self.longitude  = location.longitude
            end
          end
        rescue
          puts "can't geoloc"
        end
      end

With this code (sorry bu I show the 'tests' that - i expect - would work as fallbacks with ruby code), I just get a plain app crash if i set the airplane mode on the phone

Thanks if any experiences on making it works in this mode

回答1:

I believe the geolocation stuff runs in a separate thread, so you would want to put your begin/rescue code within the block:

BW::Location.get_once do |location|
  begin
    if location
      self.latitude   = location.latitude
      self.longitude  = location.longitude
    end
  rescue
    puts "can't geoloc"
  end
end

However, Bubblewrap will actually let you know if there was an error by setting location to a hash with one value, {error: type}. Since you're using get_once, the return value could be either a Hash or a CLLocation object, so you do need to check the object type to avoid an error:

BW::Location.get_once do |location|
  if location.is_a?(Hash) && location[:error]
    puts "can't geoloc"
  else
    self.latitude   = location.latitude
    self.longitude  = location.longitude
  end
end