【问题标题】:where to put enableHighAccuracy and other settings for geolocation在哪里放置 enableHighAccuracy 和其他地理位置设置
【发布时间】:2014-12-26 18:14:18
【问题描述】:

我有以下代码获取地理位置并发布它,然后在 div 中显示一个按钮。这适用于台式机,但不适用于 android 一个常见问题,如本网站所述。建议添加

 {frequency:5000, maximumAge: 0, timeout: 100, enableHighAccuracy:true} 

对于下面某处的代码,我尝试了几个地方只是停止工作,请问这些值应该放在哪里?

function getCoordPosition() { 

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function (position) {
    $.ajax({
        type: "POST",
        url: "{{path('login_log_location')}}",
           data: {
            latitude: position.coords.latitude,
            longitude: position.coords.longitude
        },
        success: function () {
            $("#divputinarea").html("{{path('login_log_yourarea')}}">
           <button class="btn btn-large btn-primary">
           Find </button>');
          }

           });   
       });
    }  

}

根据下面的建议,我尝试了以下方法,如果添加它,它可以在没有 { maximumAge: 0,timeout:100,enableHighAccuracy:true} 的情况下工作,我会收到错误“geolocation.getCurrentPosition 的参数 2 不可调用”

 function getCoordPosition() { 

  if (navigator.geolocation) {

  function getPosition(position) {

    $.post("{{path('login_log_location')}}",  {  
     latitude:position.coords.latitude,
     longitude:position.coords.longitude })
     .done(function(data)  {
     $("#divputinarea").html('<form method=post action="
{{path('login_log_yourarea')}}"><button class="btn btn-large   
btn-primary">Find your nearest matches</button>');

 });

      }

navigator.geolocation.getCurrentPosition(getPosition, {  
maximumAge: 0,timeout: 100,enableHighAccuracy:true} ); 

  }  

  }

 </script> 

【问题讨论】:

    标签: javascript android jquery geolocation


    【解决方案1】:

    getCurrentPosition 方法接受三个参数:成功回调、错误回调和包含配置参数的对象。

    navigator.geolocation.getCurrentPosition(
        successCallback,
        errorCallback,
        {frequency:5000, maximumAge: 0, timeout: 100, enableHighAccuracy:true} 
    ); 
    

    这是jsfiddle

    编辑:您可以修改代码来定义这三个参数。

    function getPosition(position) {
        $.post("{{path('login_log_location')}}",  {  
         latitude:position.coords.latitude,
         longitude:position.coords.longitude })
         .done(function(data)  {
             $("#divputinarea").html('<form method=post action="
        {{path('login_log_yourarea')}}"><button class="btn btn-large   
        btn-primary">Find your nearest matches</button>');
         });
      }
    
    function onError(positionError) {
        // error handling goes here
    }
    
    function getCoordPosition() { 
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(   
            getPosition, // success callback
            onError, // error callback
            {maximumAge: 0,timeout: 100,enableHighAccuracy:true} // extra params
        ); 
      }  
    }
    

    【讨论】:

    • @Nanchi 我确定你的 jsfiddle 可以工作,但我仍然找不到用我的 jquery 代码制作的方法,请参阅上面的尝试
    猜你喜欢
    • 2011-03-12
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 2012-02-21
    • 2011-05-04
    相关资源
    最近更新 更多