Search code examples
rubymotion

How do I get the coordinates of the IOS device using RubyMotion?


Just looking for the simplest example of extracting the gps coordinates - not looking to render them on a map or anything.

Thanks,


Solution

  • For iOS 8, do the following:

    1. Add these lines to your Rakefile:

      app.frameworks += ['CoreLocation']
      app.info_plist['NSLocationWhenInUseUsageDescription'] = 'I need it'
      app.info_plist['NSLocationAlwaysUsageDescription'] = 'I always need it'
      
    2. In viewDidLoad in your ViewController add:

      @locationManager = CLLocationManager.alloc.init
      @locationManager.requestWhenInUseAuthorization
      @locationManager.delegate = self
      @locationManager.startUpdatingLocation
      
    3. Implement locationManager:didUpdateLocations:

      def locationManager(manager, didUpdateLocations:locations)
        coordinate = locations[0].coordinate
        puts "lat #{coordinate.latitude}, long #{coordinate.longitude}"
      end
      
    4. If you need just the current location once, then call:

      manager.stopUpdatingLocation
      

      after getting the location in didUpdateLocations.