Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Getting GPS coordinates and saving to database

  • AnnalieF
  • AnnalieF's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
5 years 5 days ago #183247 by AnnalieF
I am trying to get GPS coordinates on page startup and saving that to the SQL database. The code that I am using, is as follows:
Code:
$(document).on('ready pjax:complete',function()
  {
    // Get question
    var q2ID = '{QID}';
    var q2 = $('#question'+q2ID);
 
    function getGPS() {
      if (navigator.geolocation)
      {
        navigator.geolocation.getCurrentPosition(showGPS, showError, { enableHighAccuracy: false, maximumAge: 0, timeout: 30000 } );
        //alert("Geolocation is not enabled");
      }
      else
      {
        //alert("Geolocation is enabled");
        $('input.text', q2).val('Geolocation is not enabled');
      }
    }
 
    function showError(error)
    {
      //alert('GPS Error: '+error.code+', '+error.message);
      {
                switch (error.code) {
                    case error.TIMEOUT:
                        //alert("The request to get user location has aborted as it has taken too long");
                        $('input.text', q2).val('Request timeout');
                        break;
                    case error.POSITION_UNAVAILABLE:
                        //alert("Location information is not available");
                        $('input.text', q2).val('No location information');
                        break;
                    case error.PERMISSION_DENIED:
                        //alert("Permission to share location information has been denied");
                        $('input.text', q2).val('Permission denied by user');
                        break;
                    default:
                        //alert("An unknown error occurred.");
                        $('input.text', q2).val('Unknown error');
                }
            }
    }
 
    function showGPS(position)
    {
      $('input.text', q2).val(position.coords.latitude+', '+position.coords.longitude);
    }
 
    getGPS();
 
  } );
</script>

The code seems to work and writes the coordinates into a short free text field, but it only works if the Google Maps button is selected in the Location -> Use mapping service section. But I don't want to display the map, I only want the coordinates. Is there any way to do this?

Also, the displayed coordinates are not saved in the database. I have tried to set the value of another field based on the value of the field in which the coordinates are displayed, but no text is displayed in the field. The code that I am using in the source section of the group is:
Code:
<script type="text/javascript" charset="utf-8">
 
$(document).ready(function() {
 
 $('#answer929736X13X17').val('{INSERTANS:929736X12X18}');
 
});
 
</script>

The latter code does work if I write the values of another short free text field, that only contains regular text, to this field.

I have already looked at the answers in the following topic:
Saving GPS coordinates

My lime survey details are: Version number: 3.17.0, build number: 190402
The topic has been locked.
  • AnnalieF
  • AnnalieF's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
5 years 3 days ago #183318 by AnnalieF
Replied by AnnalieF on topic Getting GPS coordinates and saving to database
I replaced the way the coordinates and error feedback is assigned to the field, e.g.

Code:
$('input.text', q2).val(position.coords.latitude+', '+position.coords.longitude);

replaced with
Code:
$('#answer929736X12X24').val(position.coords.latitude+', '+position.coords.longitude);

and now both problems are solved.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose