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);
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
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.
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
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
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?
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
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.
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);
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!
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 ?
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.
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;
}
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. >
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
>
> ```text
// 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);
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