Search code examples
javascriptgeolocationlimesurvey

Lime Survey - geolocation response (city, country) params returns in end url


LimeSurvey - click survey url, asks the user to allow or block location

ie. geolocation script run & return the current city & country in the end url.

It is possible or not. Below is my script, how to implement this in lime survey.

Any Suggestions, please

<script type="text/javascript"> 

  var geocoder;

  if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
  } 

   function initMap(){
    }

   //Get the latitude and the longitude;
   function successFunction(position) {
  var lat = position.coords.latitude;
  var lng = position.coords.longitude;
   codeLatLng(lat, lng)
 }

 function errorFunction(){
alert("Geocoder failed");
}

function codeLatLng(lat, lng) {

alert("Latitude: "+lat);
alert("Longtude: "+lng);

geocoder = new google.maps.Geocoder();

var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
  //console.log(results)      
    if (results[1]) {
     //formatted address                
     alert(results[0].formatted_address)         
    //find country name
    for (var i=0; i<results[0].address_components.length; i++) {
      for (var b=0;b<results[0].address_components[i].types.length;b++) {

        //there are different types that might hold a city admin_area_lvl_1 usually does in come cases looking for sublocality type will be more appropriate
            if (results[0].address_components[i].types[b] == "administrative_area_level_1") {
                //this is the object you are looking for
                city= results[0].address_components[i];
                break;
            }
            if(results[0].address_components[i].types[b] == 'country'){
              country = results[0].address_components[i];
              break;
            }
      }
    }        
    //city data
    alert(city.short_name + " " + city.long_name)
    //country data
    alert(country.short_name + " " + country.long_name)

    } else {
      alert("No results found");
    }
  } else {
    alert("Geocoder failed due to: " + status);
  }
});
 }   </script> 
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY&callback=initMap"
type="text/javascript"></script>

Any help? Thanks in advance.


Solution

  • If your code work (else : review your code first). I spoke only for LimeSurvey part.

    1. Create a multiple text question type :
      • code GEO
      • Add 2 sub question code CITY and CONTRY
    2. Add the class (using advanced settings) hidden then the question are not shown.
    3. Deactivate HTML editor and put your script inside a jquery ready (alternate solution use addScriptToQuestion plugin)
    4. Add a space or a line feed after each { you have in your js code (and a space before each } )
    5. replace you last alert (where you have city.data and coutry.data) by
      • $("#answer{SGQ}CITY").val(city.short_name + " " + city.long_name); for city
      • $("#answer{SGQ}COUNTRY").val(country.short_name + " " + country.long_name); for country
    6. You can use {GEO_CITY} and {GEO_COUNTRY} in the survey (after this page)
    7. Then you can use it in your url http://example.org/?city={GEO_CITY}&country={GEO_COUNTRY}

    Using LimeSurvey 2.50 and up version (else need some javascript to hide the question)