Getting GPS coordinates and saving to database

Plus d'informations
il y a 1 semaine 2 jours #183247 par AnnalieF
COM_KUNENA_MESSAGE_CREATED_NEW
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:
$(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:
<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

Connexion ou Créer un compte pour participer à la conversation.

LimeSurvey Partners
Plus d'informations
il y a 6 jours 8 heures #183318 par AnnalieF
COM_KUNENA_MESSAGE_REPLIED_NEW
I replaced the way the coordinates and error feedback is assigned to the field, e.g.

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

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

and now both problems are solved.

Connexion ou Créer un compte pour participer à la conversation.

Commencez dès maintenant !

Créez simplement un compte et commencez à utiliser LimeSurvey dès aujourd'hui.

Inscrivez-vous maintenant

Inscrivez-vous à notre Newsletter!