Welcome to the LimeSurvey Community Forum

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

round coordinates from GPS reading 'on the fly'

  • suselko
  • suselko's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
3 years 6 days ago #213827 by suselko
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.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 6 days ago #213829 by Joffm
To round you may use the "toFixed(x)"-method
[url] www.w3schools.com/jsref/jsref_tofixed.asp [/url]

position.coords.latitude.toFixed(2) +', '+position.coords.longitude.toFixed(2)

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • suselko
  • suselko's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
3 years 6 days ago #213833 by suselko
Thank you Joffm, that worked perfectly!
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose