Skip to main content
Question

Geolocation and form fields

  • April 4, 2018
  • 0 replies
  • 150 views

I am trying to create a button that fills in two fields on a form for a new record. I have downloaded the GeoLocate app from the exchange and am trying to use the geo.js and the get location button from this app in our timesheets app. Problem is when I click the get location button the page just reloads and doesn't fill in the form fields or pull the geolocation data from the browser. 
_______________________________________________________________________
Modified geo.js script
jQuery(document).ready(function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition);
    }
 
var test = $.ajax({url: url,async:false,success: pushResult}).responseText;
   return false;
});
function showPosition(position){
      var editURL = "";
      editURL = "bnifp6kni";
         editURL += "?act=API_EditRecord&key=" + QBU_recid;
      editURL += "&_fid_13=" + position.coords.latitude;
      editURL += "&_fid_14=" + position.coords.longitude;
 
    
      $.ajax({url: editURL,async:false});
      location.reload(true);
 }
__________________________________________________________________________
Button Formula

"javascript:" &
"var QBU_recid="& [Record ID#] &";" &
"void(jQuery.getScript('" & URLRoot() & "db/bmic8pqiv?act=dbpage&pagename=geo.js'))"

Any help on this would be greatly appreciated. 
This topic has been closed for replies.

  • Quickbase Alumni
  • April 4, 2018
Hi Jason,

A couple of things to look out for when copying an app is to make sure DBIDs and field IDs are updated. Have you updated the following to match your new app? 

I also notice that the call is using the "&key" parameter in the request. Is your table using the standard Record ID# as the key field, or has a different field been assigned as the key?

-Ryan

function showPosition(position){
      var editURL = "";
      editURL = "bnifp6kni";
         editURL += "?act=API_EditRecord&key=" + QBU_recid;
      editURL += "&_fid_13=" + position.coords.latitude;
      editURL += "&_fid_14=" + position.coords.longitude;
 
     
      $.ajax({url: editURL,async:false});
      location.reload(true);
 }
"javascript:" &
"var QBU_recid="& [Record ID#] &";" &
"void(jQuery.getScript('" & URLRoot() & "db/bmic8pqiv?act=dbpage&pagename=geo.js'))"

  • Author
  • Quickbase Alumni
  • April 4, 2018
Ryan,

Thanks for the response. I double checked and my table is using Record ID as the primary key field. I did modify the db identifiers to match my app and double checked my field ids. 
Would act=API_EditRecord work for both adding a new record and updating a record? Could this be were my error is?
thanks!

  • Quickbase Alumni
  • April 4, 2018
Hey Jason,

Awesome, the hard part is over then :-) 

Since you are using the Record ID# I would recommend using "&rid" instead of "&key". Its a subtle difference, but its important. However, when adding a record the "&rid" and "&key" parameters are unnecessary (since a key value has not been assigned yet). Below are examples on how to setup an Edit Record and an  Add Record function.

/* Edit an existing record */
function showPosition(position){
      var editURL = "bnifp6kni";
            editURL += "?a=API_EditRecord&rid=" + QBU_recid;
            editURL += "&_fid_13=" + position.coords.latitude;
            editURL += "&_fid_14=" + position.coords.longitude;
            editURL += "&apptoken=YOUR_TOKEN_HERE";
 
     
      $.ajax({url: editURL,async:false});
      location.reload(true);
 }


/* Add a new record */
function showPosition(position){
      var addURL = "bnifp6kni";
            addURL+= "?a=API_AddRecord";
            addURL += "&_fid_13=" + position.coords.latitude;
            addURL += "&_fid_14=" + position.coords.longitude;
            addURL += "&apptoken=YOUR_TOKEN_HERE";
 
     
      $.ajax({url: addURL,async:false});
      location.reload(true);
 }

  • Author
  • Quickbase Alumni
  • April 4, 2018
Thanks! I will try this and let you know if I get it working. Again thanks for the help!

  • Author
  • Quickbase Alumni
  • April 4, 2018
This works for both adding or updating a record but I am still having trouble with the position.coords.lat or long not populating the fields for lat and long. the records are updated or added to the database but not the lat and long. Any ideas?

  • Quickbase Alumni
  • April 5, 2018
Most of the above code is not ideal as it is synchronous and does not conform to modern practices.

If you are trying to fill in the latitude and longitude on an open form with a button click you can simply bind the following code to your button:

Pastie Database
https://haversineconsulting.quickbase.com/db/bgcwm2m4g?a=dr&rid=649

Here is a screenshot of me manually applying the above code through the console:


  • Author
  • Quickbase Alumni
  • April 5, 2018
Dan, thanks for your response. How would I put this in my button, as a formula? I copied the code you have and I get an error about syntax. I did change the fields to match my database fields. 

  • Quickbase Alumni
  • April 5, 2018
The demo uses the IOL technique and there is a link in the Pastie record for instrutions.

Where Am I? ~ Add New Record

https://haversineconsulting.quickbase.com/db/bnkmdpczc?a=nwr

Pastie Database
https://haversineconsulting.quickbase.com/db/bgcwm2m4g?a=dr&rid=650

Notes

(1) There really is no need for clicking the button. With a minor modification the script could easily just run as soon as the pages loads and fill in the lat and lon.

(2) Since script will be filling in the latitude and longitude you could arrange for the fields to be readonly so a pesky human could not alter the lat and lon.

(3) There are other ways to implement this so that you don't even need to have the user permit sharing his location.

  • Author
  • Quickbase Alumni
  • April 5, 2018
Dan, I think I am missing something. Can you help me with the IOL does that go in the formula or the module.js page? do I attach that to the form? Is it a field in the database?
sorry to be a pain but I am really new to this. 
thanks,
Jason. 

  • Quickbase Alumni
  • April 5, 2018
Read these posts first before using the IOL Technique on the geolocation:

https://community.quickbase.com/quickbase/topics/how-do-i-setup-the-image-onload-technique-iol

https://community.quickbase.com/quickbase/topics/what-is-the-simplest-iol-example

If you need individual help you can contact me off-world using the information in my profile:

https://getsatisfaction.com/people/dandiebolt/