hub.eb?material_id=485&track_id=482

Accessing Location Data


The Geolocation API can be used to determine the user's location. This API is available over secure (HTTPS) connections and from localhost.

The following client-side code snippet retrieves the longitude and latitude of a user.

function success(position){
  console.log(position.coords.latitude, position.coords.longitude);
}
function error(){
  console.error("Geolocation not supported.");
}
navigator.geolocation.getCurrentPosition(success, error);

See the MDN documentation for more information.

Steps


1

Create a form and add a Button Control with the text Get Location.

Create two fields called longitude and latitude and set the Client accessibilty property to read/write. Drag these fields onto the page.

2

Create a jQuery click event handler to the button and paste the following code:

function success(position){
  $eb.setFieldValue("latitude", position.coords.latitude, true);
  $eb.setFieldValue("longitude", position.coords.longitude, true);
}
function error(){
  console.error("Geolocation not supported.");
}
navigator.geolocation.getCurrentPosition(success, error);
3

Run the form and click the Get Location button. Allow the browser to access your location when prompted. 

Current Module

Related