javascript - Lime Survey - 地理位置响应(城市、国家)参数在最终网址中返回

标签 javascript geolocation limesurvey

LimeSurvey - 单击调查网址,要求用户允许或阻止位置

即。地理定位脚本运行并在最终网址中返回当前城市和国家/地区。

有可能,也有可能不可能。下面是我的脚本,说明如何在石灰调查中实现这一点。

如有任何建议,请

<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>

有什么帮助吗?提前致谢。

最佳答案

如果您的代码有效(否则:先检查您的代码)。我只代表 LimeSurvey 部分发言。

  1. 创建多文本问题类型:
    • 代码 GEO
    • 添加 2 个子问题代码 CITY 和 CONTRY
  2. 添加类(class)(使用高级设置)隐藏,则问题不会显示。
  3. 停用 HTML 编辑器并将脚本放入 jquery ready 中(替代解决方案使用 addScriptToQuestion 插件)
  4. 在 js 代码中的每个 { 之后添加一个空格或换行符(在每个 } 之前添加一个空格)
  5. 将您上次的警报(其中包含 city.data 和 coutry.data)替换为
    • $("#answer{SGQ}CITY").val(city.short_name + ""+ city.long_name); 城市
    • $("#answer{SGQ}COUNTRY").val(country.short_name + ""+ Country.long_name); 代表国家/地区
  6. 您可以在调查中使用“{GEO_CITY}”和“{GEO_COUNTRY}”(本页之后)
  7. 然后您可以在网址中使用它http://example.org/?city={GEO_CITY}&country={GEO_COUNTRY}

使用 LimeSurvey 2.50 及更高版本(否则需要一些 javascript 来隐藏问题)

关于javascript - Lime Survey - 地理位置响应(城市、国家)参数在最终网址中返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41849440/

相关文章:

javascript - 用于快速迭代的 jquery range 实用程序(原型(prototype)的 $R 等效项)

cordova - Phonegap 3.3/Cordova iOS 地理位置未找到

javascript - google maps api 地理定位和地点搜索

mysql - 如何在 LimeSurvey 中将 dd/mm/yyyy 转换为 mm/dd/yyyy

javascript - 如何在石灰调查验证中禁用 JavaScript 警报

jquery - 使用 jquery 访问 Limesurvey API - 错误 631 : failed to parse request

javascript - 使用javascript在对 Angular 线上的两个圆之间居中(x和y)svg元素

javascript - 为什么 if ('0' ==false) 和 if ('0' ) 在 Javascript 中都求值为真?

javascript - postman :您需要启用 JavaScript 才能运行此应用

ruby-on-rails - 给定特定的纬度/经度,如何计算出与纽约市附近地铁入口的距离?