Page 1 of 1

Get GPS Coordinates automatically on form submit

Posted: Tue Mar 27, 2018 12:05 am
by edydeyemi

Hi All,

I am already using the Google Maps Custom View to display users location. But now I want to capture the coordinates automatically on form submit and pass them to the Long and Lat fields in my DB. Also, I want form submission to fail if Coordinates are not captured, maybe due to user not turning GPS on.

Please, how to do this? Currently, I am using this code in Server Events -> Client Scripts -> Add Page. I am getting a location prompt but it is not passing paramaeters to my DB.

Here is my current script

function success(position) {
var lat = position.coords.latitude;
var longt = position.coords.longitude;
$("#x_lat").val(latitude);
$("#x_longt").val(longitude);
};

function error() {
$("#x_lat").val("");
$("#x_longt").val("");
};
navigator.geolocation.getCurrentPosition(success, error);

Re: Get GPS Coordinates automatically on form submit

Posted: Tue Mar 27, 2018 9:50 am
by mobhar

Press [F12] from your browser, and see whether any Javascript error message displayed.


Re: Get GPS Coordinates automatically on form submit

Posted: Mon Apr 09, 2018 5:57 pm
by edydeyemi

I run the console as advised and I get the error message below:

ReferenceError: latitude is not defined[Learn More]

Please your help will be greatly appreciated


Re: Get GPS Coordinates automatically on form submit

Posted: Mon Apr 09, 2018 6:42 pm
by mobhar

Then double check your Javascript code above, and as the error message said, then make sure you have already assigned the "latitude" variable by the valid value inside the "success" function.


Re: Get GPS Coordinates automatically on form submit

Posted: Mon Apr 09, 2018 7:58 pm
by edydeyemi

I was able to fix this., Thanks for pointing me in right direction. I also used Custom Validation to ensure form will not be submitted if Long/Lat fields are blank (enforcing users to turn GPS on and allow permission to browsers). Now, however, I want to make Long/Lat fields hidden on the form. I already set the field properties as Hidden but it is displaying on the add page. Any advise on how to do?

== Here is my Custom Validate script
function Form_CustomValidate(&$CustomError) {
$rs = $this->GetFieldValues("FormValue");
if (boolval($rs["longt"]) == null AND ($rs["lat"]) == null ) {
// Return error message in $CustomError
$CustomError = "Error! Pls turn Location/GPS On";
return FALSE;
}
return TRUE;
}

Thanks so much. I greatly appreciate your help


Re: Get GPS Coordinates automatically on form submit

Posted: Mon Apr 09, 2018 8:08 pm
by mobhar

edydeyemi wrote:
I already set the field properties as Hidden but it is displaying on the add page.
Any advise on how to do?

When you set the field as Hidden, then it will only be implemented in the Edit Page, but not in the Add Page.

To hide that field in the Add Page, then simply put the following code in "Page_Load" server event:

$this->YourFieldName->Visible = FALSE; // <-- adjust "YourFieldName" to your actual field name


Re: Get GPS Coordinates automatically on form submit

Posted: Mon Apr 09, 2018 10:41 pm
by edydeyemi

I did as you recommdended. But the coordinates are no longer passed to the Longt/Lat fields if I disable their visibility from Page_Load event. any idea what to do?


Re: Get GPS Coordinates automatically on form submit

Posted: Mon Apr 09, 2018 11:02 pm
by mobhar

Then you need to keep that field visible if you want to pass the value to that field.


Re: Get GPS Coordinates automatically on form submit

Posted: Wed Apr 18, 2018 5:04 pm
by dantanie

If you need to keep the fields visible you can wait until it loads on the page and hide it using jqery.
Go Client side events->start-up->script and put the code below
$("#x_lat").hide() ;
$("#x_longt").hide() ;

if you are doing this on a Master / Detail add form and the fields above is on the detail table
Put this script on the Master Table -> Client Scripts -> Startup

$('[data-name="lat"]').hide();
$('[data-name="longt"]').hide();

Make sure to adjust the field names to your liking


Re: Get GPS Coordinates automatically on form submit

Posted: Thu Apr 19, 2018 4:56 pm
by edydeyemi

Hi Dantanie, your suggestion worked perfectly!!! A big thanks for helping with this. What I did was to hide the fields, then pass the coordinates to them and the Custom Validation still works too. Here's my final code for anyone who might need it.

  1. Hide the Longt and Lat Fields
    // Go Client side events->start-up->script and put the code below
    $(document).ready(function(){ 
    $("#x_lat").hide() ;
    $("#x_longt").hide() ;
    });
  2. Get GPS Coordinates and pass to my Longt and Lat fields
    // Go Client side events->start-up->script and put the code after the one above
    function success(position) {
    var lat = position.coords.latitude;
    var longt = position.coords.longitude;
    $("#x_lat").val(lat);
    $("#x_longt").val(longt);
    };
    
    function error() {
    $("#x_lat").val("");
    $("#x_longt").val("");
    };
    navigator.geolocation.getCurrentPosition(success, error);
  3. Enforce users to turn GPS on and allow browser access
    //This is added on the Form_CustomValidate 
    function Form_CustomValidate(&$CustomError) {
    	 $rs = $this->GetFieldValues("FormValue"); // Get the form values as array
    	 	if (boolval($rs["longt"]) == null AND ($rs["lat"]) == null ) {
    			// Return error message in $CustomError
    			 $CustomError = "Error! Pls turn Location/GPS On";
    			return FALSE;
    		 }
    	 return TRUE;    
     }
    That's all. Once again, I am hughly grateful to Mobhar and Dantanie. Cheers!

Re: Get GPS Coordinates automatically on form submit

Posted: Sun Feb 27, 2022 4:04 am
by shailesh thakur

Hi there
1)I am trying to make a location survey table where some pictures( longblob type) are to be shot using camera( Accept-"image/*" and capture- environment. I am able to do it perfectly fine.
2) Further I need to capture the GPS location , I am using the scripts shared above and it is also working fine. This functionality I had been looking from a very long time , Thanks a ton for sharing it.
The issue is when I enable Sr 2) i.e. GPS location capture issue comes in Sr 1) Picture is NOT getting uploaded even though camera is getting enable and picture is shot ( No errors are reflecting in the debug mode or chrome console)
Pls guide what else can be the issue ?


Re: Get GPS Coordinates automatically on form submit

Posted: Sun Feb 27, 2022 9:06 am
by mobhar

What did you mean by Sr 2) and Sr 1) ? Please explain it in more detail for more discussion.


Re: Get GPS Coordinates automatically on form submit

Posted: Sun Feb 27, 2022 10:21 am
by shailesh thakur

I mean the features mentioned as Sr1) is file upload feature and Sr2) is GPS location capture Feature using the script as suggested by edydeyemi.
File upload feature is working fine when I have not used the GPS location capture (script disabled by putting // in startup script and Form Custom Validate) . Similarly GPS location capture is working fine If I not uploading any file.
What I mean is I am not able to Upload the file along ( at the same time) with the GPS location capture. Theses two features are behaving as if they can not be used together.


Re: Get GPS Coordinates automatically on form submit

Posted: Tue Mar 01, 2022 9:00 am
by mobhar

Did you separate the field between the Upload file and GPS location capture, which means you use two different fields for that?


Re: Get GPS Coordinates automatically on form submit

Posted: Sat Apr 02, 2022 11:41 am
by shailesh thakur

No, I am using the same table but I disabled the scripts for location capturing by commenting i.e. by putting //


*********************Code in Startup Script ( where "lat" and  "longt" are my fields in mysql db********************************************
// Write your table-specific startup script here, no need to add script tags.
//Hide the Longt and Lat Fields
// Go Client side events->start-up->script and put the code below
$(document).ready(function(){
$("#x_lat").hide() ;
$("#x_longt").hide() ;
});

//Get GPS Coordinates and pass to my Longt and Lat fields
// Go Client side events->start-up->script and put the code after the one above
function success(position) {
var lat = position.coords.latitude;
var longt = position.coords.longitude;
$("#x_lat").val(lat);
$("#x_longt").val(longt);
};

function error() {
$("#x_lat").val("");
$("#x_longt").val("");
};
navigator.geolocation.getCurrentPosition(success, error);
************************* code in Form custom_validate*****************

function(fobj) { // DO NOT CHANGE THIS LINE!
    // Your custom validation code here, return false if invalid.
    //This is added on the Form_CustomValidate
function Form_CustomValidate(&$CustomError) {
$rs = $this->GetFieldValues("FormValue"); // Get the form values as array
if (boolval($rs["longt"]) == null AND ($rs["lat"]) == null ) {
 Return error message in $CustomError
$CustomError = "Error! Pls turn Location/GPS On";
return FALSE;
}
    return true;
}

Re: Get GPS Coordinates automatically on form submit

Posted: Sat Apr 02, 2022 4:58 pm
by arbei

You have put server side PHP code in client side Form_CustomValidate event.


Re: Get GPS Coordinates automatically on form submit

Posted: Mon Apr 11, 2022 5:00 pm
by shailesh thakur

Thanks. I am able to get the coordinates and full functionality of my page.


Re: Get GPS Coordinates automatically on form submit

Posted: Thu Sep 12, 2024 2:46 pm
by Aburajeh

edydeyemi wrote:

Hi Dantanie, your suggestion worked perfectly!!! A big thanks to you and Mobhar for
helping with this. What I did was to hide the fields, then pass the coordinates to
them and the Custom Validation still works too. Here's my final code for anyone who
might need it.

  1. Hide the Longt and Lat Fields
    // Go Client side events->start-up->script and put the code below
    $(document).ready(function(){ 
    $("#x_lat").hide() ;
    $("#x_longt").hide() ;
    });
  2. Get GPS Coordinates and pass to my Longt and Lat fields
    // Go Client side events->start-up->script and put the code after the one above
    function success(position) {
    var lat = position.coords.latitude;
    var longt = position.coords.longitude;
    $("#x_lat").val(lat);
    $("#x_longt").val(longt);
    };
    
    function error() {
    $("#x_lat").val("");
    $("#x_longt").val("");
    };
    navigator.geolocation.getCurrentPosition(success, error);
  3. Enforce users to turn GPS on and allow browser access
    //This is added on the Form_CustomValidate 
    function Form_CustomValidate(&$CustomError) {
    	 $rs = $this->GetFieldValues("FormValue"); // Get the form values as
    array
    	 	if (boolval($rs["longt"]) == null AND ($rs["lat"]) == null ) {
    			// Return error message in $CustomError
    			 $CustomError = "Error! Pls turn Location/GPS On";
    			return FALSE;
    		 }
    	 return TRUE;    
     }
    That's all. Once again, I am hughly grateful to Mobhar and Dantanie. Cheers!

Thank you For Thats ,,, is help me and solve for me