2014-10-30 68 views
1

我有以下代碼獲取地理位置並張貼它,然後在div中顯示一個按鈕。這適用於臺式機,但不適用於本網站提及的常見問題。建議是在哪裏把enableHighAccuracy和其他設置地理位置

{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,超時:100,enableHighAccuracy:真正}如果我添加它,我得到錯誤「的說法2 geolocation.getCurrentPosition的不可調用的」

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> 

回答

0

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 
    ); 
    } 
} 
+0

@Nanchi我敢肯定你的jsfiddle的作品,但我仍然無法找到我的jQuery代碼做的方式見上嘗試 – GAV 2014-11-03 18:21:18

+0

@GAV:見我編輯的答案。 – Nachi 2014-11-04 15:57:01

相關問題