Access geolocation information from your device.
Refer to our Geolocation guide for more discussion of Geolocation, and for examples.
To use the geolocation API, you need to enable geolocation on the device. Do this by adding that capability to the build.yml file:
capabilities: - gps
We do not have a timeout parameter to automatically turn off the GPS system. If you want to turn off the GPS system, call Geolocation.turnoff.
You can call the Geolocation methods from JavaScript as well as Ruby. To use the JavaScript API, add the public/js/rho_javascript_api.js
file – created at build time as part of the application package – to the .html, .erb, or .js file calling the JavaScript method.
The JavaScript API methods with a return value can pass it as a parameter in jQuery-compatible continuation (deferred object, a kind of callback). Possible continuations to handle are done, fail, and complete.
Rho.Class.method(params).done(function(handler) { / handler… / })
You must enable JavaScript by putting rho-javascript
into extensions in your build.yml.
extensions: ["rho-javascript"]
Ruby: Geolocation.accuracy
JavaScript: Rho.Geolocation.accuracy();
def get_accuracy accuracy = Geolocation.accuracy Alert.show_popup accuracy endJavaScript
function getAccuracy(){ var accuracy = Rho.Geolocation.accuracy; alert(accuracy); }
Ruby: Geolocation.altitude
JavaScript: Rho.Geolocation.altitude();
def get_altitude altitude = Geolocation.altitude Alert.show_popup.altitude endJavaScript
function getAltitude(){ var altitude = Rho.Geolocation.altitude; alert(altitude); }
latitude1
- Latitude of the first point in degrees.longitude1
- Longitude of the first point in degrees.latitude2
- Latitude of the second point in degrees.longitude2
- Longitude of the second point in degrees.
Ruby: Geolocation.haversine_distance(latitude1, longitude1, latitude2, longitude2)
JavaScript: Rho.Geolocation.haversine_distance(latitude1, longitude1, latitude2, longitude2);
Ruby: Geolocation.known_position?
JavaScript: Rho.Geolocation.is_known_position();
Ruby: Geolocation.latitude
JavaScript: Rho.Geolocation.latitude();
Ruby: Geolocation.longitude
JavaScript: Rho.Geolocation.longitude();
def get_satellites satellites = Geolocation.satellites Alert.show_popup.satellites endJavaScript
function getSatellites(){ var satellites = Rho.Geolocation.satellites; alert(satellites); }
Ruby: Geolocation.satellites
JavaScript: Rho.Geolocation.satellites();
Set callback to track location changes.
You only need to call Geolocation.set_notification once. The current behavior of the callback is that it will be called forever until it is stopped; you need to call Geolocation.turnoff to stop it. The previous behavior was that the callback was called once and needed to be reset. |
The callback_url parameter must be set in order for Geolocation to function. |
Ruby: Geolocation.set_notification(callback_url, callback_params, ping_gpssystem_interval)
JavaScript: Rho.Geolocation.set_notification(callback, callback_params, ping_gpssystem_interval);
callback_url
- url for the callback method called upon a location change notification or for the interval set by ping_gpssystem_interval.callback_params
- a string added to the body of the callback url. You can use it to identity who is setting up the callback, such as "my_tag=55". In general you do not set callback_param (leave it blank as in "").ping_gpssystem_interval
- (optional) If 0, the system interval is used; the callback is executed when the GPS system processes a location update (dependent on the mobile platform). If set to a number (such as 3), the callback is executed at an interval of this number of seconds (such as every three seconds).When the Geolocation.set_notification callback is called, it will receive a variable called @params, just like a normal Rhodes controller action. Here are the parameters included in the @params variable.
known_position
- 1 or 0. Return from known_position? method.latitude
- Return from call to latitude method.longitude
- Return from call to longitude method.available
- 1 if geolocation is available, 0 otherwise. For 1, not only does the hardware exist, but also the user can turn GPS off in phone settings, or not allow GPS activity on iPhone, etc.status
- "ok" or "error"error_code
- error code from RhoError.accuracy
- horizontal radius in meters; iOS and Android.There is no valid data related to distance, which the Geolocation API exposes in WM and hence there is no support for extended notification in WM. |
minDistance and minTimeout values are optional; if nothing is provided, whenever a location update is processed by the underlying system, it is sent to the application. It is supported only on Android devices. |
url
- Callback method to be invoked on any notification.callbackParams
- To be used when callback is invoked. It is generally used to know the source of the callback. This is optional.options
- This is a hash and can consist of the following two keys:minDistance
- Minimum moving distance(in meters) to invoke the callback again.minTimeout
- Minimum timeout for next callback invocation.Ruby: Geolocation.set_notification_ex(callback url, string callbackParams, options hash)
JavaScript: Rho.Geolocation.set_notification_ex(callback url, string callbackParams, options hash);
When the callback happens, it will provide the following parameters:
known_position
- 1 or 0. Return from known_position? method.latitude
- Value returned by Geolocation.latitude()
method.longitude
- Value returned by Geolocation.longitude()
method.altitude
- Value returned by Geolocation.altitude()
method.speed
- Value returned by Geolocation.speed()
method.accuracy
- Value returned by Geolocation.accuracy()
method.satellites
- Value returned by Geolocation.satellites()
method.available
- 1 if geolocation is available, 0 otherwise. For 1, not only does the hardware exist, but also the user can turn GPS off in phone settings, or not allow GPS activity on iPhone, etc.status
- "ok" or "error"error_code
- error code from RhoError.Units: Meters/sec over ground
Ruby: Geolocation.speed
JavaScript: Rho.Geolocation.speed();
def get_speed speed = Geolocation.speed Alert.show_popup speed end
function getSpeed(){ var speed = Rho.Geolocation.speed; alert(speed); }
When you call Geolocation.turnoff() , after the GPS is switched off, you might still receive a few callbacks (this depends on the platform; iOS and Android does not receive callbacks after turnoff). |
Geolocation.turnoff
JavaScript
Rho.Geolocation.turnoff();