I have a smartphone-based survey and at the beginning I check if respondents are located in a given town. Although this location check happens with their informed consent, ideally I don't want to collect data on their exact location, to avoid data protection / security issues. Something like +/- 500 meters will do the job. I think it is possible to enhance my code below to achieve that, by rounding the coordinates to the nearest second decimal (e.g. '55.5123456;-3.276543' would be rounded to '55.51;-3.28'). I guess this line needs amending:
$('input:hidden[id^="answer"]', q1).val(position.coords.latitude+';'+position.coords.longitude);
But I can't code in javascript. I'd be very grateful for your help.
<script type="text/javascript" charset="utf-8">
$(document).on('ready pjax:scriptcomplete',function(){
// Identify this question
var q1ID = '{QID}';
var q1 = $('#question'+q1ID);
// Disable the "Next" button
$('#ls-button-submit').prop('disabled', true);
function getGPS() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showGPS, gpsError);
} else {
$('input.text', q1).val('No GPS Functionality');
$('input:hidden[id^="answer"]', q1).val('No GPS Functionality');
}
}
function gpsError(error) {
alert('The option \'Location Services\' is turned off in your phone. Go into your phone\'s \'Settings\', then \'Privacy\', then \'Location Services\' and turn it on. Then tap on the survey link again.');
}
function showGPS(position) {
$('input.text', q1).val(position.coords.latitude+', '+position.coords.longitude);
$('input:hidden[id^="answer"]', q1).val(position.coords.latitude+';'+position.coords.longitude);
// Enable the "Next" button
$('#ls-button-submit').prop('disabled', false);
}
getGPS();
});
</script>
The topic has been locked.